if 语句后可以跟可选的 else if ... else 语句,这对于使用单个if ... else if语句测试各种条件非常有用。

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 - 示例

/* 全局变量定义 */
int A=5 ;
int B=9 ;
int c=15;

Void setup () {

}

Void loop () {
   /* 检查布尔条件 */
   if (A > B) /* 如果条件为真,则执行以下语句*/{
      A++;
   }
   /* 检查布尔条件 */
   else if ((A == B )||( B < c) ) /* 如果条件为真那么执行以下语句*/{
      C=B* A;
   }else
      c++;
}

参考链接

https://www.learnfk.com/arduino/arduino-if-else-if-else-statement.html