3. The third form is if-else-if form
The first two forms of if statements are generally used in two branches. When you have more than one branch selection, you can take the IF-ELSE-IF statement in the form of an
if (expression 1)
Statement 1;
Else if (expression 2)
Statement 2;
Else if (expression 3)
Statement 3;
...
Else if (expression m)
Statement m
Else
statement n;
Its semantics is to determine the value of an expression in turn, and then execute its corresponding statement when a value is true. It then jumps to the entire if statement to continue executing the program. If all of the expressions are false, the statement n is executed. Then continue to follow the procedure. The execution of the IF-ELSE-IF statement is shown in Figure 3-3.
#include "stdio.h"
Void Main () {
char C;
printf ("Input a Character:");
C=getchar ();
if (c<32)
printf ("This was a control character\n");
else if (c>= ' 0 ' &&c<= ' 9 ')
printf ("This is a digit\n");
else if (c>= ' a ' &&c<= ' Z ')
printf ("This is the capital letter\n");
else if (c>= ' a ' &&c<= ' z ')
printf ("This is a small letter\n");
else
printf ("This is a other character\n");
This example requires distinguishing the categories of keyboard input characters. Types can be judged according to the ASCII code of the input character. A control character with an ASCII value less than 32 is known by the ASCII code table. Numbers between "0" and "9" are uppercase letters between "a" and "Z", with lowercase letters between "a" and "Z" and others in other characters. This is a multiple-branch selection problem, using IF-ELSE-IF statement programming, to determine the input character of the ASCII code in the range, respectively, give different output. For example, enter "G", and the output displays it as lowercase characters.
4. The following issues should also be noted in the use of IF statements
(1) in three forms of if statements, after the IF keyword is an expression. The expression is usually a logical expression or a relational expression, but it can also be another expression, such as an assignment expression, or even a variable. For example: if (a=5) statements, if (b) statements, are allowed. As long as the value of the expression is not 0, it is true. As in if (a=5) ... ; The value of an expression is always 0, so the following statement is always executed, which is not necessarily the case in the program but is syntactically valid.
Another example is a program segment: if (a=b)
printf ("%d", a);
Else
printf ("a=0"); The semantics of this statement is to give a value of B to a, or output the value if it is not 0, otherwise the output "a=0" string. This usage is often present in the program.
(2) In an if statement, a conditional judgment expression must be enclosed in parentheses, and a semicolon must be added after the statement.
(3) In the three forms of an IF statement, all statements should be a single statement, and if you want to execute a set of statements when the condition is met, you must enclose the set of statements in {} to form a compound statement. Note, however, that you cannot add a semicolon after the}.
For example:
if (a>b) {
a++;
b++;
}
else{
a=0;
b=10;
}
Nesting of IF statements
When the execution statement in an If statement is an if statement, the case of the IF statement nesting is formed. The general form can be expressed as follows: an
if (expression)
If statement,
or a
if (expression)
If statement, or a
else
if statement, or a
if statement within a nested if-else type. This will cause multiple if and multiple else overlaps, paying special attention to if and else pairing problems. For example,
if (expression 1)
if (expression 2)
Statement 1;
Else
Statement 2;
What else is the if pairing with?
Should be understood as: or should be understood as:
if (expression 1) if (expression 1)
if (expression 2) if (expression 2)
Statement 1; statement 1;
Else else if
Statement 2; statement 2;
To avoid this ambiguity, the C language stipulates that Else is always paired with the nearest if before it, so the above example should be understood in the previous case. The
compares the size relationships of two numbers.
Void Main () {
int a,b;
printf ("Please input a,b:");
scanf ("%d%d", &a,&b);
if (a!=b)
if (a>b) printf ("a>b\n");
else printf ("a<b\n");
Else printf ("a=b\n");
}
This example uses the nested structure of the IF statement. In essence, the nested structure is for multiple branch selection, and in example 3.16 there are actually three choices, namely A>b, A<b or a=b. This problem can also be done with if-else-if statements. And the program is clearer. As a result, the nested structure of the IF statement is less commonly used. To make the program easier to read and understand.
Void Main () {
int a,b;
printf ("Please input a,b:");
scanf ("%d%d", &a,&b);
if (a==b) printf ("a=b\n");
Else if (a>b) printf ("a>b\n");
Else printf ("a<b\n");
}
Conditional operators and conditional expressions
Conditional expressions can often be used to implement a single assignment statement in a conditional statement. Not only make the program concise, but also improve the operational efficiency.
The conditional operator is? And: It is a three-mesh operator, that is, the amount of three participating operations. The general form of a conditional expression consisting of a conditional operator is:
Expression of 1? Expression 2: Expression 3
The evaluation rule is: If the value of the expression 1 is true, the value of the expression 2 is used as the value of the conditional expression, otherwise the value of the expression 2 is used as the value of the entire conditional expression. A conditional expression is usually used in an assignment statement.
For example, a conditional statement:
if (a>b) max=a;
else max=b;
Available conditional expressions are written as max= (a>b)? a:b; The semantics for executing the statement is that, if A>b is true, A is given to Max, or B is given to Max.
When you use conditional expressions, you should also note the following points:
1. The operation precedence of the conditional operator is lower than the relational and arithmetic operators, but is higher than the assignment character. So max= (a>b) a:b can remove parentheses and write as Max=a>b?a:b
2. The conditional operator? And: is a pair of operators that cannot be used separately.
3. The binding direction of the conditional operator is from right to left.
For example:
A>b?a:c>d?c:d should be understood as
A>b?a: (C>D?C:D) This is the case where the conditional expression is nested, where the expression 3 is a bar
A piece of expression.
void Main () {
int A,b,max;
printf ("\ n input two numbers:");
scanf ("%d%d", &a,&b);
printf ("max=%d", a>b?a:b);
}
The above example is reprogrammed with a conditional expression to output a large number of two digits.