目录

If…else if …else statement

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

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

  • 一个if可以有零个或一个else语句,它必须在任何其他if之后。

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

  • 一旦else if成功,其余的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 else语句执行顺序

如果Else If Else声明

例子 (Example)

/* 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++;
}
↑回到顶部↑
WIKI教程 @2018