Arduino If ... else if ... else语句

2018-11-02 18:27 更新

if 语句后面可以跟随一个可选的 else if ... else 语句,其对于测试各种条件非常有用。

当使用 if ... else if ... else 语句时,请记住:

  • 一个 if 可以有0或一个else语句,它必须在所有else if之后。

  • if 可以有0到多个else if语句,它们必须在else之前。

  • 一旦 else if 成功,将不会测试剩余的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语句执行顺序


if ... else if ... else语句执行顺序

例子

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


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号