Arduino教程22.Arduino 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++;
}

发布者:suiyublg,转转请注明出处:https://huibian.net/1008-5/

(0)
suiyublg的头像suiyublg
上一篇 2017年3月24日 17:51
下一篇 2017年3月24日 17:55

相关推荐

  • Arduino教程06.Arduino 变量和常量

    在我们开始解释变量类型之前,我们需要确定一个非常重要的主题,称为变量范围。

    2017年3月22日
    69002
  • Arduino教程26.Arduino do … while循环

    do … while 循环类似于while循环。在while循环中,循环连续条件在循环开始时测试,然后再执行循环体。do … while语句在执行循环体之后测试循环连续条件。因此,循环体将被执行至少一次。 当 do … while 终止时,将使用while子句后的语句继续执…

    2017年3月25日
    50300
  • Arduino进阶07.Arduino 串行外设接口

    串行外设接口(SPI)总线是用于串行通信的系统,最多可使用四个导体,通常为三个。一个导体用于数据接收,一个导体用于数据发送,一个导体用于同步,另一个导体用于选择与之通信的设备。它是一个全双工连接,这意味着数据是同时发送和接收的。最大波特率高于I2C通信系统中的波特率。

    2017年4月11日
    99066
  • Arduino教程24.Arduino 条件运算符? :

    条件运算符 ? : 是C语言中唯一的三元运算符。 ? :条件运算符语法 expression1 ? expression2 : expression3 首先评估expression1。如果其值为true,那么将评估expression2,并忽略expression3。如果expression1评估为false,则将评估expression…

    2017年3月24日
    53200
  • Arduino教程33.Arduino micros()函数

    micros()函数返回Arduino板开始运行当前程序时的微秒数。该数字在大约70分钟后溢出,即回到零。在16 MHz Arduino板(例如Duemilanove和Nano)上,此函数的分辨率为4微秒(即返回值总是4的倍数)。在8 MHz Arduino板(例如LilyPad)上,此函数的分辨率为8微秒。 micros()函数语法 micros () ;…

    2017年3月29日
    55200

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
欢迎大家来到大雄学编程!