if...else...end 語(yǔ)句語(yǔ)法:
MATLAB 中一個(gè) if ... else 語(yǔ)句的語(yǔ)法示例:
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
如果布爾表達(dá)式的值為 “true”,那么執(zhí)行 if 的代碼塊;如果布爾表達(dá)式的值為 “false”,else 的代碼塊將被執(zhí)行。
if...else...end 語(yǔ)句流程圖:
詳細(xì)例子如下:
在MATLAB中建立一個(gè)腳本文件,并輸入下述的代碼:
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í)行上述代碼,產(chǎn)生下述結(jié)果:
a is not less than 20 value of a is : 100
更多建議: