MATLAB - if... end 語句



一個if ... end語句由一個if語句和一個布林表示式以及一個或多個語句組成。它以end語句作為分隔符。

語法

MATLAB 中 if 語句的語法如下:

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

如果表示式計算結果為真,則將執行 if 語句內的程式碼塊。如果表示式計算結果為假,則將執行 end 語句之後的第一個程式碼集。

流程圖

MATLAB if statement

示例

建立一個指令碼檔案並輸入以下程式碼:

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_decisions.htm
廣告

© . All rights reserved.