1. 主页
  2. 文档
  3. Arduino教程(持续更新)
  4. 4.编程
  5. 4.5 控制语句

4.5 控制语句

判断结构要求程序员指定要由程序评估或测试的一个或多个条件,以及条件为真时要执行的语句(必需的)和条件为假时要执行的语句(可选的)。

以下是大多数编程语言中典型的判断结构的一般形式:

4.5 控制语句

控制语句是源代码中控制程序执行流程的元素。它们是:

序号控制语句和描述
1If 语句它采用括号中的表达式,后面跟随语句或语句块。如果表达式为真,则执行语句或语句块,否则跳过这些语句。
2If … else 语句一个 if 语句后面可以跟随一个可选的else语句,当表达式为false时执行。
3If … else if … else 语句if 语句后面可以跟随一个可选的 else if … else 语句,其对于测试各种条件非常有用。
4switch case 语句类似于if语句, switch … case 通过允许程序员指定应在各种条件下执行的不同代码来控制程序的流程。
5条件运算符 ? :条件运算符? :是C语言中唯一的三元运算符。

If语句

它采用括号中的表达式,后面跟随语句或语句块。如果表达式为真,则执行语句或语句块,否则跳过这些语句。

不同形式的if语句

形式1

if (expression)
   statement;

如果你有一个语句,你可以使用没有大括号{}的if语句。

形式2

if (expression) {
   Block of statements;
}

if语句 – 执行顺序

4.5 控制语句

例子

/* Global variable definition */
int A = 5 ;
int B = 9 ;

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 */
   If ( ( A < B ) && ( B != 0 )) /* if condition is true then execute the following statement*/ { 
      A += B;
      B--;
   }
}

Arduino If … else语句

一个 if 语句后面可以跟随一个可选的else语句,当表达式为false时执行。

if … else语句语法

if (expression) {
   Block of statements;
}
else {
   Block of statements;
}

if … else语句 – 执行顺序

4.5 控制语句

例子

/* Global variable definition */
int A = 5 ;
int B = 9 ;

Void setup () {

}

Void loop () {
   /* check the boolean condition */
   if (A > B) /* if condition is true then execute the following statement*/ {
      A++;
   }else {
      B -= A;
   }
}

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语句执行顺序

4.5 控制语句

例子

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

Arduino switch case语句

类似于if语句, switch … case 通过允许程序员指定应在各种条件下执行的不同代码来控制程序的流程。特别是, switch 语句将变量的值与 case 语句中指定的值进行比较。当发现一个case语句的值与变量的值匹配时,运行case语句中的代码。

switch语句使用 break 关键字退出,通常在每个case语句的结尾使用。如果没有break语句,switch语句将继续执行后续的表达式(“fall-through”),直到到达break语句或达到switch语句的结尾。

switch case语句语法

switch (variable) { 
   case label:
   // statements
   break;
}

case label: { 
   // statements
   break;
}

default: { 
   // statements
   break;
}

switch case语句执行顺序

4.5 控制语句

例子

这里是一个简单的switch的例子。假设我们有一个只有3个不同状态(0,1或2)的变量阶段以及与每个状态相对应的函数(事件)。以下是我们如何将代码切换到相应的例行程序:

switch (phase) {
   case 0: Lo(); break;
   case 1: Mid(); break;
   case 2: Hi(); break;
   default: Message("Invalid state!");
}


Arduino 条件运算符? :

条件运算符 ? : 是C语言中唯一的三元运算符。

? :条件运算符语法

expression1 ? expression2 : expression3

首先评估expression1。如果其值为true,那么将评估expression2,并忽略expression3。如果expression1评估为false,则将评估expression3,而expression2将被忽略。结果将是expression2或expression3的值,具体取决于它们中的哪一个结果为True。

条件运算符从右到左关联。

示例

/* Find max(a, b): */
max = ( a > b ) ? a : b;
/* Convert small letter to capital: */
/* (no parentheses are actually necessary) */
c = ( c >= 'a' && c <= 'z' ) ? ( c - 32 ) : c;

条件运算符规则

  • expression1必须是标量表达式;expression2和expression3必须遵守以下规则之一。
  • 这两个表达式都必须是算术类型的。
  • expression2和expression3进行通常的算术转换,决定结果的类型。
  • 两个表达式都必须是void类型。结果类型为void。
这篇文章对您有用吗?

我们要如何帮助您?

发表回复

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