if...else...end 語句語法:
MATLAB 中一個 if ... else 語句的語法示例:
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
如果布爾表達式的值為 “true”,那么執(zhí)行 if 的代碼塊;如果布爾表達式的值為 “false”,else 的代碼塊將被執(zhí)行。
if...else...end 語句流程圖:

詳細例子如下:
在MATLAB中建立一個腳本文件,并輸入下述的代碼:
a = 100; % check the boolean condition if a < 20 % if condition is true then print the following fprintf('a is less than 20 ' ); else % if condition is false then print the following fprintf('a is not less than 20 ' ); end fprintf('value of a is : %d ', a);
編譯和執(zhí)行上述代碼,產生下述結果:
a is not less than 20 value of a is : 100
更多建議: