Matlab if...end語句

一個if...end語句由一個if語句和一個布爾運算式組成,後跟一個或多個語句。它是由end語句分隔的語句塊。

語法

在MATLAB中,if語句的語法是 -

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

如果運算式(expression)計算結果為true,則if語句中的代碼塊將被執行。如果運算式的計算結果為false,那麼執行結束語句後的第一組代碼。

流程圖

例子

創建腳本檔並鍵入以下代碼 -

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

上一篇: Matlab決策 下一篇: Matlab迴圈