You should also be aware of the following issues in using the IF statement: in three forms of an if statement, after the IF keyword is an expression. The expression is usually a logical expression or a relational expression, but it can be another expression, such as an assignment expression, or even a variable. For example:
if (a=5) statement;
if (b) statement;
are allowed. As long as the expression has a value other than 0, it is true. As in:
if (a=5) ...;
The values in the expression are always non-0, so the statements that follow are always executed, although this is not necessarily the case in the program, but is syntactically legal.
Nesting of IF statements
When the EXECUTE statement in an IF statement is another if statement, it forms the case where the IF statement is nested. Pay particular attention to the pairing of if and else issues.
To avoid this ambiguity, the C language specifies that else is always paired with the nearest if.
The following points should also be noted when using switch statements:
- The value of each constant expression after the case cannot be the same, otherwise an error occurs.
- After the case, multiple statements are allowed and can be enclosed without {}.
- The order of the case and default clauses can be changed without affecting the results of the program execution.
- The default clause can be omitted.
The C language has four loops: a loop consisting of a goto statement, a while loop, a do-while loop, and a for loop.
The comparison of the four types of loops:
Each of the four loops can be used to deal with the same problem, and can generally be substituted for each other. But generally do not promote the use of the Goto cycle, not only because of its crappy, we also strongly recommend that you do not use the goto statement, try to use other statements instead.
While and Do-while loops, the loop body should include statements that tend to end the loop.
The FOR statement has the strongest and most common features.
When using the while and do-while loops, the operation of the loop variable initialization should be done before the while and Do-while statements, while the for statement can implement the initialization of the loop variable in expression 1.
Both break and continue statements can be used in loops to jump out of loops (end loops), and break statements can also be used in a switch statement to jump out of a switch statement.
Break statement
Break statements are typically used in loop statements and switch statements. When break is used in switch statements, the program can jump out of a switch and execute a switch statement, and if there is no break statement, it becomes a dead loop and cannot exit. The use of break in switch has been encountered in the example described earlier in the switch statement, which is no longer an example.
When the break statement is used in a do-while, for, and while Loop statement, the program terminates the loop and executes the statement following the loop, usually the break statement is always associated with the IF statement, which jumps out of the loop when the condition is met.
Attention:
- The break statement has no effect on the If-else conditional statement;
- In a multilayer loop, a break statement jumps only one layer outward.
Continue statements
The continue statement acts by skipping the remaining statements in the loop body and forcing the next loop to be executed. The continue statement is used only in the loop body of for, while, Do-while, and often with an if condition statement to speed up the loop.
C-Language Process Control