1.3 Control Flow Statement
Turbo C2.0 provides rich and flexible control flow statements, including conditional statements, circular statements, and switch statements. These statements are described in detail below.
1.3.1 Conditional statement
Like any other language, Turbo C2.0 also provides conditional statements. The general form of conditional statements in Turbo C2.0 is:
if (expression)
Statement 1;
Else
Statement 2;
The above structure indicates that if the value of the expression is not 0 (ture), then the statement 1 is executed, execution of statement 1 begins after statement 2, and if the expression has a value of 0 (false) that is false, the statement 1 is skipped and statement 2 is executed. An expression is a combination of a relational expression and a logical expression, as described earlier in the expression, and is not repeated here.
Attention:
1. In the conditional execution statement "Else statement 2;" Part is the selection, which can be by default, when the conditional statement becomes:
if (expression) statement 1;
Represents the execution of statement 1 if the value of the expression is 0, otherwise skip statement 1 to continue execution.
2. If statement 1 or statement 2 has more than one statement to execute, the statements must be included with "{" and "}", when the conditional statement is in the form of:
if (expression)
{
Statement body 1;
}
Else
{
Statement body 2;
}
3. Conditional statements can be nested, as is often the case, but conditional nested statements are prone to errors because of the main reason for not knowing which if is corresponding to which else.
For example:
if (x>20| | X<-10)
if (y<=100&&y>x)
printf ("good");
Else
printf ("bad");
For the above scenario, Turbo C2.0 stipulates that the Else statement matches the most recent if statement, and else in the example above matches the if (y<=100&&y>x). In order to make else and if (x>20| | X<-10), the curly braces must be used. As shown below:
if (x>20| | X<-10)
{
if (y<=100&&y>x)
printf ("good");
}
Else
printf ("bad");
4. Available ladder type IF-ELSE-IF structure.
The general form of ladder type structure is:
if (expression 1)
Statement 1;
else if (expression 2)
Statement 2;
else if (expression 3)
Statement 3;
.
.
.
Else
Statement N;
This structure is to judge the condition from top to bottom, and once the condition is found, execute the statement related to it and Man the rest of the ladder; if none of the conditions are met, the last Else statement n is executed. This last else often acts as a "default condition."
Similarly, if more than one statement in each condition is to be executed, the statements must be included with "{" and "}".