Arduino If ... else if ... else語句

2018-11-02 18:27 更新

if 語句后面可以跟隨一個(gè)可選的 else if ... else 語句,其對(duì)于測(cè)試各種條件非常有用。

當(dāng)使用 if ... else if ... else 語句時(shí),請(qǐng)記住:

  • 一個(gè) if 可以有0或一個(gè)else語句,它必須在所有else if之后。

  • if 可以有0到多個(gè)else if語句,它們必須在else之前。

  • 一旦 else if 成功,將不會(huì)測(cè)試剩余的else if或else語句。

if ... else if ... else語句

if (expression_1) {
   Block of statements;
}

else if(expression_2) {
   Block of statements;
}
.
.
.

else {
   Block of statements;
}

if ... else if ... else語句執(zhí)行順序


if ... else if ... else語句執(zhí)行順序

例子

/* 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++;
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)