/* 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语句 – 执行顺序
例子
/* 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语句执行顺序
例子
/* 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语句中的代码。
/* 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;
目录
判断结构要求程序员指定要由程序评估或测试的一个或多个条件,以及条件为真时要执行的语句(必需的)和条件为假时要执行的语句(可选的)。
以下是大多数编程语言中典型的判断结构的一般形式:
控制语句是源代码中控制程序执行流程的元素。它们是:
它采用括号中的表达式,后面跟随语句或语句块。如果表达式为真,则执行语句或语句块,否则跳过这些语句。
一个 if 语句后面可以跟随一个可选的else语句,当表达式为false时执行。
if 语句后面可以跟随一个可选的 else if … else 语句,其对于测试各种条件非常有用。
类似于if语句, switch … case 通过允许程序员指定应在各种条件下执行的不同代码来控制程序的流程。
条件运算符? :是C语言中唯一的三元运算符。
If语句
它采用括号中的表达式,后面跟随语句或语句块。如果表达式为真,则执行语句或语句块,否则跳过这些语句。
不同形式的if语句
形式1
如果你有一个语句,你可以使用没有大括号{}的if语句。
形式2
if语句 – 执行顺序
例子
Arduino If … else语句
一个 if 语句后面可以跟随一个可选的else语句,当表达式为false时执行。
if … else语句语法
if … else语句 – 执行顺序
例子
Arduino If … else if … else语句
if 语句后面可以跟随一个可选的 else if … else 语句,其对于测试各种条件非常有用。
当使用 if … else if … else 语句时,请记住:
if … else if … else语句
if … else if … else语句执行顺序
例子
Arduino switch case语句
类似于if语句, switch … case 通过允许程序员指定应在各种条件下执行的不同代码来控制程序的流程。特别是, switch 语句将变量的值与 case 语句中指定的值进行比较。当发现一个case语句的值与变量的值匹配时,运行case语句中的代码。
switch语句使用 break 关键字退出,通常在每个case语句的结尾使用。如果没有break语句,switch语句将继续执行后续的表达式(“fall-through”),直到到达break语句或达到switch语句的结尾。
switch case语句语法
switch case语句执行顺序
例子
这里是一个简单的switch的例子。假设我们有一个只有3个不同状态(0,1或2)的变量阶段以及与每个状态相对应的函数(事件)。以下是我们如何将代码切换到相应的例行程序:
Arduino 条件运算符? :
条件运算符 ? : 是C语言中唯一的三元运算符。
? :条件运算符语法
首先评估expression1。如果其值为true,那么将评估expression2,并忽略expression3。如果expression1评估为false,则将评估expression3,而expression2将被忽略。结果将是expression2或expression3的值,具体取决于它们中的哪一个结果为True。
条件运算符从右到左关联。
示例
条件运算符规则