I. The C language has two optional structures:
(1): if statement, used to implement the selection structure of two branches;
(2): switch Statement, which is used to select the structure of multiple branches;
II. if statement:
The general format of the if statement is as follows:
If (expression)
Statement 1;
Or:
If (expression ){
Statement 1;
}
Note:
[1]: The expression in the if statement can be a relational expression, a logical expression, or even a numeric expression. It returns a logical value, that is, true or false!
III. All forms are as follows:
(1): single judgment statement
If (expression 1)
Statement 1;
Or:
If (expression 1 ){
Statement 1;
}
(2): Dual-branch statement
If (expression 1)
Statement 1;
Else
Statement 2;
Or:
If (expression 1 ){
Statement 1;
} Else {
Statement 2;
}
(3): multi-branch statements
If (expression 1)
Statement 1;
Else if (expression 2)
Statement 2;
......
Else if (expression n)
Statement n;
Else
Statement n + 1;
Note:
[1]: the entire statement can be written on multiple or one line;
Example:
If (x> 0) y = 1; else y = 0;
[2]: if statements are written on multiple rows, which are a whole and belong to one statement. Do not mistakenly think that the if part is a statement, and the else part is another statement. When compiling the if statement in the system, check whether
Whether an else statement exists. if an else statement exists, it is used as the part of the if statement. if no else statement exists, the if statement has ended! Note that the else statement cannot end as a separate statement. It must be
If statement pairing is used, and it is paired with the nearest if statement. Use the proximity principle!
[3]: if statements do not add parentheses and only have constraints on the first sentence of code. The subsequent code is not restricted and will be executed in order; if braces are added, all codes in the brackets are restricted.
4. Select structure statement nesting
If statement nesting: In an if statement, one or more if statements are nested.
The format is as follows:
(1): nesting the if statement in the if statement
If (expression 1 ){
If (expression 2 ){
Statement 1;
}
}
(2): Nested dual-branch statements in the if statement
If (expression 1 ){
If (expression 2 ){
Statement 1;
} Else {
Statement 2;
}
}
(3): Nested multi-branch statements in the if statement
If (expression 1 ){
If (expression 2 ){
Statement 1;
} Else if (expression 3 ){
Statement 2;
} Else if (expression 4 ){
Statement 3;
} Else {
Statement 4;
}
}
V. Use switch statements to implement multi-branch statements
The switch statement is a multi-branch selection statement.
Switch statement: Jump the process to different statements based on the value of the expression.
6. The general form is as follows:
(1): no break statement form
Switch (expression ){
Case constant 1:
Statement 1;
Case constant 2:
Statement 2;
Case constant 3:
Statement 3;
Default:
Statement 4;
}
(2): in the form of a break statement
Switch (expression ){
Case constant 1:
Statement 1;
Break;
Case constant 2:
Statement 2;
Break;
Case constant 3:
Statement 3;
Break;
Default:
Statement 4;
Break;
}
Note:
[1]: The "expression" after the switch statement. Its value type should be INTEGER (including character) [character type also belongs to the integer form and is stored in the computer according to the ASCII code]
[2]: the braces under the switch statement are a composite statement. It must be marked. If it is not entered, an error is returned! There are several statements that combine the case keyword and default. The value after case must be a constant,
Or a constant expression. Used as a label to identify a location.
[3]: If there is no break statement, it is executed in sequence. If there is a break statement, the statement is executed in which case, then immediately exit the entire switch statement.
[4]: running principle of the switch statement: when executing the switch statement, first calculate the value of the expression after the switch, and then compare it with the case labels, if it is the same as a constant in a case label
The process is forwarded to the statement following the case label. If no switch expression matches the case constant. Check whether there are any default statements. If yes, execute the statement following the default label. If no, return
Output the entire switch statement.
[5]: the order of case labels does not affect the result. The default statement can appear first, and other case statements can be disrupted.
[6]: each case constant must be different from each other. Otherwise, there will be a conflict. Repeat the definition system and an error will be reported!
[7]: All the statements after the case label statement are automatically executed without adding brackets under the case statement!
[8]: Multiple case labels can be used in a group of execution statements. As follows:
Case:
Case B:
Case C:
Printf ("pass! "); Break;
7. nested multi-branch selection statements:
(1): The switch statement can be nested in the switch.
Switch (expression 1 ){
Case constant 1:
Switch (expression 2 ){
Case constant 1:
Statement 1;
Break;
Case constant 2:
Statement 2;
Break;
}
& Nbs