判断结构要求程序员指定要由程序或测试的一个或条件,以及条件为真时要执行的语句(必要的)和条件为假时要执行的语句(任选的)。
下面是大多数编程语言中典型的判断结构的一般形式:

控制语句是源代码中控制程序执行流程的元素。
| 顺序 | 控制语句和描述 |
|---|---|
| 1 | 如果语句如果表达式为真,则语句或语句块,否则可以避免这些语句。 |
| 2 | 如果…… else语句一个 if 语句可以判断一个任选的else语句,当表达式为假时执行。 |
| 3 | if … else if … else语句if 某某可以预测一个选择的 else … else ,其各种条件非常有用。 |
| 4 | 切换大小写语句事件触发, 切换…… 通过程序允许指定在各种条件下执行的不同代码来控制程序的流程。 |
| 5 | 条件一致?:条件一致?:是C语言中唯一的三元对立。 |
如果表达式为真,则语句或语句块,否则可以避免这些语句。
形式1
if (expression) statement;
如果你有一个语句,你可以使用没有大括号{}的if语句。
形式2
if (expression) {
Block of statements;
}

/* 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--;
}
}
一个 if 语句可以判断一个任选的else语句,当表达式为假时执行。
if (expression) {
Block of statements;
}
else {
Block of statements;
}

例子
/* 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;
}
}
if 某某可以预测一个选择的 else … else ,其各种条件非常有用。
当使用 if … else if … else 语句时,请记住:
if (expression_1) {
Block of statements;
}
else if(expression_2) {
Block of statements;
}
.
.
.
else {
Block of statements;
}

/* 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++;
}
事件判断语句, 切换…… 通过编程允许指定在各种条件下执行的不同代码来控制程序的流程。特别是, 切换 语句将变量的值与 事件 语句中指定的值进行比较。一个case语句的值与变量的值匹配时,运行case语句中的代码。
开关语句使用 破 关键字退出,通常在每个情况下语句的结尾使用。如果没有破语句,开关语句将继续执行后续的表达式(“落空”),直到到达断裂语句或达到开关语句的结尾。
switch (variable) {
case label:
// statements
break;
}
case label: {
// statements
break;
}
default: {
// statements
break;
}

例子
这里是一个简单的切换例子。如果我们还有一个只有 3 个不同状态(0,1 或 2 个)的变量阶段以及与每个事件状态相对的函数(事件)。例行程序:
switch (phase) {
case 0: Lo(); break;
case 1: Mid(); break;
case 2: Hi(); break;
default: Message("Invalid state!");
}
条件一致?: 是C语言中唯一的三元对立。
expression1 ? expression2 : expression3
首先评估表达式1。如果其值为真,那么将评估表达式2,并忽略表达式3。表达式1评估为假,则评估表达式3,而表达式2将被忽略。的哪一个结果为真。
条件匹配从右到左关联。
示例
/* 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;