目录

if ... end statement

if ... end语句由if语句和布尔表达式后跟一个或多个语句组成。 它由end声明分隔。

语法 (Syntax)

MATLAB中if语句的语法是 -

if <expression>
   % statement(s) will execute if the boolean expression is true 
   <statements>
end

如果表达式的计算结果为true,那么将执行if语句中的代码块。 如果表达式求值为false,则将执行end语句之后的第一组代码。

流程图 (Flow Diagram)

MATLAB if语句

例子 (Example)

创建一个脚本文件并键入以下代码 -

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
↑回到顶部↑
WIKI教程 @2018