The code we see earlier is executed sequentially, that is, the first statement is executed first, then the second, the third ... Until the last statement.
However, in many cases, sequential structure of the code is far from enough, such as a program to limit the use of adults only, children because of age, not the right to use. At this point the program needs to make a judgment to see if the user is an adult, and give a hint.
If-else statement
In the C language, use the IF and else keywords for judgment. Please look at the following code first:
#include <stdio.h>
int main ()
{
int age;
printf ("Please enter your Age:");
scanf ("%d", &age);
if (age>=18) {
printf ("Congratulations, you are an adult, you can use the software!") \ n ");
} else{
printf ("Sorry, you are underage, not suitable to use the software!") \ n ");
}
return 0;
}
Possible results of the operation:
Please enter your age: 23
Congratulations, you are an adult, you can use the software!
The structure of the IF Else statement is:
if (expression) {
Statement Block 1
}else{
Statement Block 2
}
This means: If the value of the expression is true, the statement block 1 is executed, otherwise the statement block 2 is executed. The execution process can be expressed as the following illustration:
The so-called statement block (Statement blocks) is a collection of one or more statements surrounded by {}. If there is only one statement in the statement block, you can also omit {}, for example:
if (age>=18) printf ("Congratulations, you have grown up, you can use the software!") \ n ");
else printf ("Sorry, you are underage, not suitable to use the software!") \ n ");
Since the IF Else statement can execute different code depending on the situation, it is also called a branching structure or selection structure, with two branches in the above code.
Find the larger value in two numbers:
#include <stdio.h>
int main ()
{
int a, b, Max;
printf ("Enter two integers:");
scanf ("%d%d", &a, &b);
if (a>b) max=a;
else max=b;
The larger values for printf ("%d and%d are:%d\n", A, B, max);
return 0;
}
Run Result:
Enter two integers: 34 28
The larger values of 34 and 28 are: 34
In this case, with the variable max, Max is used to hold the larger value, and finally the max is output.
If statement
We can use only the IF statement. That is, if else does not have to appear at the same time. The basic form of an if statement is:
if (expression) {
Statement block
}
This means that if the value of the expression is true, the following block of statements is executed, otherwise skip directly. The process can be expressed as the following illustration:
Use only the IF statement to find a larger value in two numbers:
#include <stdio.h>
int main ()
{
int a, b, Max;
printf ("Enter two integers:");
scanf ("%d%d", &a, &b);
Max=b; Assuming B maximum if
(a>b) max=a;//If a>b, then change Max's value
printf (the larger values for%d and%d are:%d\n, a, B, max);
return 0;
}
Run Result:
Enter two integers: 34 28
The larger values of 34 and 28 are: 34
In this example program, enter two numbers a, B. Assign b first to the variable max, then use the IF statement to determine the size of Max and B, such as Max less than B, and B to max. So Max is always a large number, and finally the value of Max is output.
Multiple if Else statements
The IF Else statement can also be used concurrently to form multiple branches, as follows:
if (expression 1) {
Statement Block 1
else if (expression 2) {
Statement Block 2
}else if (expression 3) {
Statement Block 3
}else if (expression m) {
Statement block M
}else{
Statement block N
}
This means that the value of an expression is judged sequentially, and the corresponding statement is executed when a value is true. It then jumps to the entire if statement to continue executing the program. Executes the statement block N if all of the expressions are false. Then continue to follow the procedure. The execution of multiple if else statements is as shown in the following illustration:
For example, to determine the type of character entered:
#include <stdio.h>
int 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");
return 0;
}
Run Result:
Input a Character:e
This is a small letter
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, which is programmed with multiple if else statements to determine the range in which the ASCII code of the input character is located, giving different output respectively. For example, the input is "E", and the output shows it as lowercase characters.
You should also be aware of the following issues when using the IF statement:
1 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 also 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 value of the expression is not 0, it is true. As in:
if (a=5) ...;
The value of the expression in is always 0, so the following statement is always executed, which is not necessarily the case in the program but is syntactically valid.
Also, if there is a program section:
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, the conditional judgment expression must be enclosed in parentheses.
3 The statement block is surrounded by {}, but be aware that after} do not add a semicolon;. For example:
if (a>b) {
a++;
b++;
} else{
a=0;
b=10;
}
Nesting of IF statements
An If statement can also be nested, for example:
#include <stdio.h>
int main () {
int a,b;
printf ("Input two numbers:");
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");
}
return 0;
}
Run Result:
Input two Numbers:12 68
A<b
If the IF statement is nested, pay attention to the pairing of if and else. The C language stipulates that else is always paired with the most recent if, for example:
if (a!=b)//①
if (a>b) printf ("a>b\n");//②
Else printf ("a<b\n");//③
③ and ② are paired, not paired with ①.
The above is the C language if else knowledge of the detailed, I hope to help learn C language friends.