if 語(yǔ)句后面可以跟隨一個(gè)可選的 else if ... else 語(yǔ)句,其對(duì)于測(cè)試各種條件非常有用。
當(dāng)使用 if ... else if ... else 語(yǔ)句時(shí),請(qǐng)記住:
一個(gè) if 可以有0或一個(gè)else語(yǔ)句,它必須在所有else if之后。
if 可以有0到多個(gè)else if語(yǔ)句,它們必須在else之前。
一旦 else if 成功,將不會(huì)測(cè)試剩余的else if或else語(yǔ)句。
if (expression_1) { Block of statements; } else if(expression_2) { Block of statements; } . . . else { Block of statements; }
/* Global variable definition */ int A = 5 ; int B = 9 ; int c = 15; Void setup () { } Void loop () { /* check the boolean condition */ if (A > B) /* if condition is true then execute the following statement*/ { A++; } /* check the boolean condition */ else if ((A == B )||( B < c) ) /* if condition is true then execute the following statement*/ { C = B* A; }else c++; }
更多建議: