JSRUN 用代码说话

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

编辑教程

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

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++;
}
JSRUN闪电教程系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的闪电教程,让知识的积累变得统一完整、自成体系。 大家可以一起参与进共编,让零散的知识点帮助更多的人。
X
支付宝
9.99
无法付款,请点击这里
金额: 0
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
如有疑问请联系QQ:565830900
正在生成二维码, 此过程可能需要15秒钟