Conditional expression statement
1. if statement
if (expression) statement 1;
If the value of the expression is If it is non-0, execute statement 1, otherwise skip the statement and continue executing the following statements.
If statement 1 has more than one statement to be executed, {} must be used to include these statements. In this case, the conditional statement is in the form:
if (expression)< /p>
{
Statement body 1, 2, 3;
}
For example:
if(x> =0) y=x;
if(a||b&&c)
{
z=a+b;
c+ =z;
}
if--else statement
In addition to specifying certain statements to be executed when the condition is true, you can also execute the statement when the condition is false. execute another piece of code. Use the else statement in the C statement to achieve this purpose.
if (expression) statement 1;
else statement 2;
Similarly, when statement 1 or statement 2 is more than one statement, you need to use {} enclose statements. For example:
if(x>=0) y=x;
else y=-x;
if--else if--else structure< /p>
if(expression 1)
Statement 1;
else if(expression 2)
Statement 2;
else if(expression 3)
Statement 3;
.
else
Statement n;
This structure judges the conditions one by one from top to bottom. Once the condition is found to be satisfied, the statements related to it are executed and other remaining steps are skipped; if no condition is satisfied, the last else statement is executed. n. The last else often serves as the default condition. Likewise, if more than one statement is to be executed in each condition, these statements must be included using {}.
Conditional statements can be nested. This situation is often encountered, but nested conditional statements are prone to errors. The main reason is that we don’t know which if corresponds to which else.
For example:
if(x>20||x<-10)
if(y<=100&&y>x)
printf(Good);
else
printf(Bad);
For the above situation, Turbo C stipulates: else statement matches the most recent if statement , else in the above example matches if(y<=100&&y>x). In order to make else match if(x>20||x<-10), curly braces must be used. As shown below:
if(x>20||x<-10)
{
if(y<=100&&y>x)
printf(Good);
}
else
printf(Bad);
Here are a few examples:
1. Enter a number, if it is greater than 0, output plus; if it is a negative number, output negative; if it is exactly 0, output zero.
main()
{
float num;
scanf(%f,&f);
if(num>0)
printf(plus\n);
else if(num<0)
printf(negative\n);
else if(num<0)
printf(negative\n);
p>
else
printf(zero\n);
}
First define two variables, then enter a number, and then judge this number range, output the corresponding string.
2. Input a number x and output y. where y is the absolute value of x.
main()
{
float x,y;
scanf(%f,&x);
< p>if(x>=0) y=x;else y=-x;
printf(%f\n,y);
}
Trubo C 2.0 has written some commonly used functions, we just need to use them. For example, the function of finding absolute values ??is available in the C library.
See below:
#include math.h
main()
{
float x,y;
scanf(%f,&x);
y=fabs(x); /*Find the absolute value of x, and then assign it to y*/
printf(%f \n,y);
}
This program performs exactly the same function as the above program, both of which find the absolute value. It can be seen that the following method is better than the above.
Since fabs() is a function that comes with the system, when using it, we must include the library file math.h where it is located in the program, which is the first line of the program. Similar ones include finding the square root sqrt(), finding the exponential power exp(), etc. These functions related to mathematics are all in math.h.
3. Input x, output y, x and y satisfy the relationship:
x<-5 y=x;
-5<=x<1 y=2*x+5;
1<=x<4 y=x+6;
x>=4 y=3*x-2;
The program is as follows:
main()
{
float x,y;
scanf(%f,&x) ;
if(x<-5)
y=x;
else if(-5<=x&&x<1)
y=2*x+5;
else if(1<=x&&x<4)
y=x+6;
else
y=3*x-2;
printf(%f\n,y);
}
Two points should be noted here:
(1).-5<=x&&x<1
Cannot be written as -5<=x<1;1<=x&&x<4, nor can it be written as 1<=x<4 ;In C language, continuous inequalities cannot be recognized.
(2).y=2*x+5 cannot be written as y=2x+5; y=3*x-2 cannot be written as y=3x-2; this is different from the way we usually write it no the same.
4. Enter three numbers x, y, z, and then output them from large to small.
main()
{
float x,y,z;
scanf(%f%f%f,&x, &y,&z); if(x>=y&&x>=z) {
printf(%f\t,x);
if(y>=z) printf(% f\t%f\n,y,z); else printf(%f\t%f\n,z,y); }
else if(y>=x&&y>=z) {
printf(%f\t,y);
if(x>=z) printf(%f\t%f\n,x,z); else printf( %f\t%f\n,z,x); }
else
{
printf(%f\t,z);
p>if(x>=y) printf(%f\t%f\n,x,y); else printf(%f\t%f\n,y,x); }
}
Note: This is a typical nested structure of if statements. If parentheses are not used, the corresponding relationship between if and else will be messed up.
switch--case statement
When writing programs, we often encounter multi-path problems that are divided according to different situations. In this case, nested if -else-if statements can be used to solve the problem. Implemented, but if-else-if statements are inconvenient to use and prone to errors. For this situation, Turbo C provides a switch statement.
The format of the switch statement is:
switch(variable)
{
case constant 1:
Statement 1 or empty;
case constant 2:
Statement 2 or empty;
.
case constant n:
Statement n or empty;
default:
Statement n+1 or empty;
}
Execute switch statement When , compare the variables one by one with the constants after case. If they are equal to one of them
, the statement under the constant will be executed. If they are not equal to any constant, the statement after default will be executed.
Note:
1. The variable in switch can be a numerical value or a character, but it must be an integer.
2. Some cases and defaults can be omitted.
3. The statement after each case or default can be a statement body, but it does not need to be enclosed in {}. For example:
main()
{
intx,y;
scanf(%d,&x);
witch(x)
{
case 1:
y=x+1;
break; /*Exit Switch statement, exit only when break is encountered*/
case 4:
y=2*x+1;
break;
default:
y=x--;
break;
printf(%d\n,y);
}
Programs written with switch statements can definitely be done with if statements. So under what circumstances do you need to use the switch statement? It is generally used when a comparative integer occurs or when it can be converted into a comparative integer. Look at the following example:
Example: A student's score is divided into five grades. Those with more than 90 points are 'A', 80-89 are 'B', 70-79 are 'C', and 60- A score of 69 is a 'D' and a score below 60 is an 'E'. Now input a student's grades and output his grade.
(1). Use if statement
main()
{
float num;
char grade;
scanf(%d,&num);
if(num>=90) grade='A';
else if(num>= 80&&num<89) grade='B';
else if(num>=70&&num<79) grade='C';
else if(num>=60&&num<69) grade='D';
else grade='E';
printf(%c,grade);
}
(2). Use switch statement
main()
{
intnum;
char grade;
scanf(%d,&num);
num/=10;
switch(num)
{
case 10:
case 9:
grade='A';
break;
case 8:
grade ='B';
break;
case 7:
grade='C';
break;
case 6:
grade='D';
break;
default:
grade='E';
break;
}
printf(%c,grade);
}
To explain, Not every case has a statement in it, sometimes it is empty, just like this question. The order of execution of the switch statement is judged from the first case. If it is correct, it will be executed until break; if it is incorrect, it will execute the next case.
So here, when the score is 100 points, execute case 10: and then execute below, grade='A';break;exit