MATLAB 流程控制
-
流程控制
流程控制要求程序员应指定一个或多个要由程序评估或测试的条件,如果确定条件为真,则应指定要执行的一个或多个语句,如果条件为真,则可以选择要执行的其他语句。条件确定为假。以下是大多数编程语言中常见的典型决策结构的一般形式-MATLAB提供以下类型的决策声明。 -
if...end
语法-if <expression> % statement(s) will execute if the boolean expression is true <statements> end
创建一个脚本文件并输入以下代码-
尝试一下a = 10; % check the condition using if statement if a < 20 % if condition is true then print the following fprintf('a is less than 20\n' ); end fprintf('value of a is : %d\n', a);
运行文件时,它将产生以下结果-a is less than 20 value of a is : 10
-
if...else..end
语法-if <expression> % statement(s) will execute if the boolean expression is true <statement(s)> else <statement(s)> % statement(s) will execute if the boolean expression is false end
创建一个脚本文件并输入以下代码-
尝试一下a = 100; % check the boolean condition if a < 20 % if condition is true then print the following fprintf('a is less than 20\n' ); else % if condition is false then print the following fprintf('a is not less than 20\n' ); end fprintf('value of a is : %d\n', a);
运行文件时,它将产生以下结果-a is not less than 20 value of a is : 100
-
if...elseif...elseif...else...end
语法-if <expression 1> % Executes when the expression 1 is true <statement(s)> elseif <expression 2> % Executes when the boolean expression 2 is true <statement(s)> Elseif <expression 3> % Executes when the boolean expression 3 is true <statement(s)> else % executes when the none of the above condition is true <statement(s)> end
创建一个脚本文件并输入以下代码-
尝试一下a = 100; %check the boolean condition if a == 10 % if condition is true then print the following fprintf('Value of a is 10\n' ); elseif( a == 20 ) % if else if condition is true fprintf('Value of a is 20\n' ); elseif a == 30 % if else if condition is true fprintf('Value of a is 30\n' ); else % if none of the conditions is true ' fprintf('None of the values are matching\n'); fprintf('Exact value of a is: %d\n', a ); end
运行文件时,它将产生以下结果-None of the values are matching Exact value of a is: 100
-
嵌套if
在MATLAB中嵌套if-else语句始终是合法的,这意味着您可以在另一个if or elseif语句中使用一个if or elseif语句。语法-if <expression 1> % Executes when the boolean expression 1 is true if <expression 2> % Executes when the boolean expression 2 is true end end
创建一个脚本文件并输入以下代码-
尝试一下a = 100; b = 200; % check the boolean condition if( a == 100 ) % if condition is true then check the following if( b == 200 ) % if condition is true then print the following fprintf('Value of a is 100 and b is 200\n' ); end end fprintf('Exact value of a is : %d\n', a ); fprintf('Exact value of b is : %d\n', b );
运行文件时,它将产生以下结果-Value of a is 100 and b is 200 Exact value of a is : 100 Exact value of b is : 200
-
switch 语句
MATLAB中switch语句的语法为-语法-switch <switch_expression> case <case_expression> <statements> case <case_expression> <statements> ... ... otherwise <statements> end
创建一个脚本文件并输入以下代码-
尝试一下grade = 'B'; switch(grade) case 'A' fprintf('Excellent!\n' ); case 'B' fprintf('Well done\n' ); case 'C' fprintf('Well done\n' ); case 'D' fprintf('You passed\n' ); case 'F' fprintf('Better try again\n' ); otherwise fprintf('Invalid grade\n' ); end
运行文件时,它将产生以下结果-Well done
-
嵌套 switch 语句
MATLAB中switch语句的语法为-语法-switch(ch1) case 'A' fprintf('This A is part of outer switch'); switch(ch2) case 'A' fprintf('This A is part of inner switch' ); case 'B' fprintf('This B is part of inner switch' ); end case 'B' fprintf('This B is part of outer switch' ); end
创建一个脚本文件并输入以下代码-
尝试一下a = 100; b = 200; switch(a) case 100 fprintf('This is part of outer switch %d\n', a ); switch(b) case 200 fprintf('This is part of inner switch %d\n', a ); end end fprintf('Exact value of a is : %d\n', a ); fprintf('Exact value of b is : %d\n', b );
运行文件时,它将产生以下结果-This is part of outer switch 100 This is part of inner switch 100 Exact value of a is : 100 Exact value of b is : 200