content
branch statement (select structure)
Example of an if else statement
The role of continue in for loop
The role of continue in do...while
The role of break in do...while
branch statement (select structure)
In life, we will encounter many selective problems, as shown in the following figure
In C language, how to implement this selection method?
if statement
Syntax structure: Mode 1: if (expression)
statement;
Way 2:
if(expression)
statement 1;
else
statement 2;
Way 3:
if(expression1)
statement 1;
else if(expression2)
statement 2;
else
statement 3 ;
Example of an if statement
int main()
{
int age = 0;
scanf("%d", &age);
if(age<18)
{
printf("未成年\n");
}
}
Enter an age, if the age is less than 18 then print Minor
Example of an if else statement
#include <stdio.h>
int main()
{
int age = 0;
scanf("%d", &age);
if(age<18)
{
printf("未成年\n");
}
else
{
printf("成年\n");
}
}
Enter an age, if the age is less than 18 print the minor, if the age is greater than 18 print the adult
if else if else example
#include <stdio.h>
int main()
{
int age = 0;
scanf("%d", &age);
if(age<18)
{
printf("少年\n");
}
else if(age>=18 && age<30)
{
printf("青年\n");
}
else if(age>=30 && age<50)
{
printf("中年\n");
}
else if(age>=50 && age<80)
{
printf("老年\n");
}
else
{
printf("老寿星\n");
}
}
Enter age, if age < 18, print Junior
Age >= 18 and age < 30, print youth
Age >= 30 and age < 50, print middle age
age >= 50 and age < 80, print old age
If the age is not in the above fields, print the old birthday
Note: If the expression statement after if or else or else if exceeds one sentence, we use {} to enclose the statement belonging to if or else or else if
Analyzing conditions
In the C language, 0 is false, non-0 is true, when the content in the select statement () is true, the expression of the current statement will be executed.
else and if matching problem
#include <stdio.h>
int main()
{
int a = 0;
int b = 2;
if(a == 1)
if(b == 2)
printf("hehe\n");
else
printf("haha\n");
return 0;
}
We found that nothing was printed at this time, this is because, a=0, cannot enter the if (a==1) expression, at this time if (b==2) is an expression belonging to if (a==1) , so if (b==2) cannot be executed, the else here is matched with if (b==2), that is, the else is matched with the if that is closest to it and belongs to the same scope.
correct input
#include <stdio.h>
int main()
{
int a = 0;
int b = 2;
if(a == 1)
{
if(b == 2)
{
printf("hehe\n");
}
}
else
{
printf("haha\n");
}
return 0;
}
switch statement
A switch statement is also a branching statement. Often used in multi-branch situations.
For example: from 1-7, every time you enter a number, print the corresponding week. If the if statement has to be used 7 times, it is a bit troublesome. At this time, we can use another statement called the switch statement
switch statement format, switch(integer expression)
{ case integer constant expression: statement; }
Enter a number from 1-7 and print the corresponding week
#include <stdio.h>
int main()
{
int day = 0;
switch(day)
{
case 1:
printf("星期一\n");
break;
case 2:
printf("星期二\n");
break;
case 3:
printf("星期三\n");
break;
case 4:
printf("星期四\n");
break;
case 5:
printf("星期五\n");
break;
case 6:
printf("星期六\n");
break;
case 7:
printf("星期天\n");
break;
}
return 0;
}
The function of break: If you enter 1, and there is no break after printing Monday, the program will automatically go down and enter case2 until it encounters a break to jump out of the program or until the end of the switch statement.
But what if the value entered is out of range after the case statement? In C language, when the value in switch() has no corresponding case, we can use the defalut statement.
Function of the default statement: When the value input in switch() has no corresponding conditions in the case, that is, when it exceeds the scope of the case, this statement will enter the default statement uniformly. The position of default in the switch statement will affect the output The result has no effect, but generally people put it at the end of switch{} by default.
Matters needing attention: switch (integer expression), the content in () can be an integer expression or a char type, because the character type itself is also an integer, this is because it is still stored in ASCII code value, case integer constant Expression: statement; The expression after the case must be a constant, not a variable, and an error will be reported if it is a variable.
switch nesting
switch statement allows nested use
#include <stdio.h>
int main()
{
int n = 1;
int m = 2;
switch (n)
{
case 1:
m++;
case 2:
n++;
case 3:
switch (n)
{//switch允许嵌套使用
case 1:
n++;
case 2:
m++;
n++;
break;
}
case 4:
m++;
break;
default:
break;
}
printf("m = %d, n = %d\n", m, n);
return 0;
}
loop statement
while() loop
while(expression)
loop statement;
prints 1-10 on the screen
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
printf("%d ", i);
i = i+1;
}
return 0;
}
while(expression)
loop statement;
When using the while statement, the expression is first judged. If the expression is true, the loop statement is executed. If the expression is false, the while loop is not entered.
break and continue
After we use break in the loop
Originally, 1-10 was printed, but after using break, only 1-4 was printed. This is because there is an if (i==5) after entering the while loop. Here, it is judged whether i is 5, and if it is 5, the break statement is executed , when i==5, the break statement is executed, and no numbers are printed after that, because the function of the break statement is to jump out of the current loop
After we use continue in the loop
The cursor is still flashing
Here, when continue is used, print; 1-4, print 5 without continue, this is because when continue is not used, there is a judgment condition (i==5) to judge whether i is equal to 5, if i is equal to 5 at this time, After printing, after using continue, the program will automatically jump to while() and then go down. When continue is encountered again, it still jumps to while and then goes down, so i will always be equal to 5 at this time.
for loop
for(expression1; expression2; expression3)
loop statement;
Expression 1 Expression 1 is the initialization part, which is used to initialize the loop variable.
Expression 2 Expression 2 is the conditional judgment part, which is used to judge the termination of the loop.
Expression 3 Expression 3 is an adjustment part for adjustment of loop conditions.
print 1-10 with a for loop
for loop flow chart
The for loop executes expression 1 at the beginning, and then judges expression 2. If the second is true, execute the loop statement, then execute expression 3, and know that expression 2 is false to stop the loop
The role of continue in for loop
After using continue in the for statement, when the statement executes continue, the program will continue to execute expression 3
The role of break in for loop
After using continue in the for statement, when the statement executes break, the program will directly jump out of the for loop
for loop features
The for loop can omit expressions 1, 2, and 3, but when expression 2 is omitted, the condition of expression 2 is always established by default, and it will enter an infinite loop as shown in the figure below.
The for loop can also create a variable in statement 1 and assign it a value, but it is generally not recommended to use the for loop in this way
for loop lightning protection
#include <stdio.h>
int main()
{
int i = 0;
int k = 0;
for (i = 0, k = 0; k = 0; i++, k++)
k++;
return 0;
}
This program will not enter the for loop, because expression 2 is an assignment statement instead of a judgment statement, assign 0 to k, and k is false at this time, so the program will never enter the for loop
do...while() loop
do
{
loop statement;
}while(expression);
The do...while loop is to execute the statement in the loop first, and then judge the condition. If the condition is true, continue to execute the loop, otherwise jump out of the loop
The role of continue in do...while
When i is equal to 5, execute the continue statement. At this time, the program will always execute if (i==5) and continue, so the cursor keeps blinking at this time.
The role of break in do...while
At this time, the cursor after 4 stops blinking, indicating that the program is completed. When i==5, execute break, and the program will jump out of the loop directly.
goto statement
The C language provides goto statements and labels that mark jumps that can be abused at will. Theoretically, the goto statement is unnecessary. In practice, it is easy to write code without the goto statement. But in some cases the goto statement is still useful, the most common usage is to terminate the processing of the program in some deeply nested structures. For example: jump out of two or more layers of loops at a time. The use of break in this case of multi-layer loop will not achieve the purpose. It can only exit from the innermost loop to the previous loop.
The goto statement is that when the statement is executed, after encountering the goto statement, it will automatically jump to the place indicated by the goto statement, and the program will then be executed. The following is the correct use of the goto statement
for(...)
for(...)
{
for(...)
{
if(disaster)
goto error;
}
}
…
error:
if(disaster)
// 处理错误情况
Example
We find the command prompt in the computer and enter shutdown -s -t 60, which means that the computer will start a 60s countdown to shut down after this statement is executed. When you enter shutdown -a again, the shutdown command will be cancelled.
However, the goto statement has certain limitations. We try to use the goto statement as little as possible, and replace the goto statement with other statements. stdlib.h is the header file of system, and the header file of strcmp is string.h
#include <stdio.h>
#include <stdlib.h>
int main()
{
char input[10] = {0};
system("shutdown -s -t 60");
while(1)
{
printf("电脑将在1分钟内关机,如果输入:我是猪,就取消关机!\n请输入:>");
scanf("%s", input);
if(0 == strcmp(input, "我是猪"))
{
system("shutdown -a");
break;
}
}
return 0;
}