Table of contents
1. What is a statement?
There are five types of C language statements: expression statements, function call statements, control statements, compound statements, and empty statements .
Control statements are used to control the execution flow of the program to implement various structural methods of the program . C language has three structures: sequence structure, selection structure, and loop structure . There are nine types of control statements, which can be divided into three categories: conditional judgment statements (branch statements): if statements and switch statements ; loop statements: do while statement, while statement and for statement ; turn statement: break statement, goto statement, continue statement and return statement .
2. Branch statement
2.1 if statement
Grammatical structure of if statement
if (expression) //If the expression is true, the statement is executed. In C language, 0 is false and non-0 is true.
{
statement;
}
if(expression)
{
statement;
}
else //else matches the nearest if
{
statement;
}
//Multiple branches
if(expression)
{
statement;
}
else if(expression)
{
statement;
}
else
{
statement;
}
Example
#include <stdio.h>
int main()
{
int age = 0;
scanf("%d", &age);
if(age<18)
{
printf("未成年\n");
}
}
#include <stdio.h>
int main()
{
int age = 0;
scanf("%d", &age);
if(age<18)
{
printf("未成年\n");
}
else
{
printf("成年\n");
}
}
#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("middle-aged\n");
}
else if(age>=50 && age<80)
{ printf("old age\n"); } else { printf("old birthday star\n"); } }
2.2 switch statement
Switch is also a branch statement, often used in multi-branch situations.
For example, enter or exit a number and output the corresponding day of the week.
switch statement
switch(expression)
{
statement item;
}
statement item
case integer constant expression:
statement;
In the switch statement, we cannot directly implement the branch. Only by using break can we achieve the real branch.
Example
#include <stdio.h>
int main()
{ int day = 0; switch(day) { case 1: printf("Monday\n"); break; //The break statement divides multiple statement items into different The branch part of case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
}
return 0;
}
If you are asked to input the numbers 1-5, weekday is output, and the numbers 6-7 are input, weekend is output. Then there is the following code
#include <stdio.h>
int main()
{
int day = 0;
switch(day)
{
case 1:case 2:
case 3:
case 4:
case 5:
printf("weekday\n");
break;
case 6:
case 7:
printf("weekend\n");
break;
}
return 0;
}
If the value of the expression does not match the values of all case tags, you can use the default tag. When the value of the switch expression does not match the value of all case tags, the statement following the default clause will be executed. Each switch Only one default clause can appear in the statement.
Example
#include <stdio.h>
int main()
{ int day = 0; switch(day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break ; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;default:
printf("Illegal input\n");
break;
}
return 0;
}
3. Loop statements
Loops are used when an operation is to be performed multiple times.
3.1 while loop
while(expression)
{ Loop statement;
}
Example: Print numbers 1-10
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
printf("%d ", i);
i = i+1;
}
return 0;
}
break in while
#include <stdio.h>
int main()
{ int i = 1; while(i<=10) { if(i == 5) break; //When break is encountered in the loop, terminate the loop directly
printf("%d ", i);
i = i+1;
}
return 0;
}
continue in while
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
if(i == 5){
i = i+1;
//continue terminates this loop, jumps directly to the judgment part of while, and performs the judgment of the next loopcontinue
;}
printf("%d ", i);
i = i+1;
}
return 0;
}
3.2 for loop
for(expression1; expression2; expression3)
{loop statement;
}
Expression 1 initializes the loop variable, Expression 2 determines the condition to determine when the loop terminates, and Expression 3 adjusts the loop condition.
Example of printing numbers 1-10
#include <stdio.h>
int main()
{
int i = 0;
for(i=1; i<=10; i++)
{
printf("%d ", i);
}
return 0;
}
Execution flow chart
break and continue have the same effect in a for loop as in a while loop.
3.3 do...while loop
do
{loop statement;
}
while(expression);
Execution flow chart
Features: The loop is executed at least once, the usage scenarios are limited, and it is not used frequently.