使用 if... elseif...elseif...else 語句,要注意以下幾點:
一個 if 可以有零個或多個 else,但是它必須跟在 elseif 后面(即只有 elseif 存在才會有 else)。
一個 if 可以有零個或多個 elseif ,必須出現(xiàn)else。
一旦 elseif 匹配成功,余下的 elseif 將不會被測試。
W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
MATLAB 的 if...elseif...elseif...else...end 語句中 if 語句可以跟隨一個(或多個)可選的 elseif... else 語句,這是非常有用的,可以用來對各種條件進行測試。
使用 if... elseif...elseif...else 語句,要注意以下幾點:
一個 if 可以有零個或多個 else,但是它必須跟在 elseif 后面(即只有 elseif 存在才會有 else)。
一個 if 可以有零個或多個 elseif ,必須出現(xiàn)else。
一旦 elseif 匹配成功,余下的 elseif 將不會被測試。
if <expression 1> % Executes when the expression 1 is true <statement(s)> elseif <expression 2> % Executes when the boolean expression 2 is true <statement(s)> Elseif <expression 3> % Executes when the boolean expression 3 is true <statement(s)> else % executes when the none of the above condition is true <statement(s)> end
在MATLAB中建立一個腳本文件,并輸入下述代碼:
a = 100; %check the boolean condition if a == 10 % if condition is true then print the following fprintf('Value of a is 10 ' ); elseif( a == 20 ) % if else if condition is true fprintf('Value of a is 20 ' ); elseif a == 30 % if else if condition is true fprintf('Value of a is 30 ' ); else % if none of the conditions is true ' fprintf('None of the values are matching '); fprintf('Exact value of a is: %d ', a ); end
編譯和執(zhí)行上述代碼,產(chǎn)生如下結(jié)果:
None of the values are matching Exact value of a is: 100
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: