Wiki
Program Flow Structure
1.Program flow structure
C/C++Supports the three most basic program execution structures: ==sequential structure, selection structure, and loop structure==
- Sequential structure: the program executes in order, without jumps.
- Selection structure: based on whether a condition is satisfied, the corresponding function is executed selectively.
- Loop structure: based on whether a condition is satisfied, a certain block of code is executed repeatedly multiple times.
1.1.Selection Structure
1.1.1.if statement
**Purpose:** Execute statements that meet the condition.
Three forms of the if statement
-
Single-line format if statement
-
Multi-line format if statement
-
if statement with multiple conditions
-
Single-line format if statement:
if(条件){ 条件满足执行的语句 }
Example:
int main() { //选择结构-单行if语句 //输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印 int score = 0; cout << "请输入一个分数:" << endl; cin >> score; cout << "您输入的分数为: " << score << endl; //if语句 //注意事项,在if判断语句后面,不要加分号 if (score > 600) { cout << "我考上了一本大学!!!" << endl; } return 0; }Execution result (example input
650):请输入一个分数: 您输入的分数为: 650 我考上了一本大学!!!
Note: Do not add a semicolon after an if conditional expression.
- Multi-line format if statement:
if(条件){ 条件满足执行的语句 }else{ 条件不满足执行的语句 };

Example:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
int score = 0;
cout << "请输入考试分数:" << endl;
cin >> score;
if (score > 600)
{
cout << "我考上了一本大学" << endl;
}
else
{
cout << "我未考上一本大学" << endl;
}
// 返回 0 表示程序正常结束。
return 0;
}
Run result (the following is an example when entering 10 , 20 , 30... in sequence as prompted):
请输入考试分数:
我未考上一本大学
- Multi-condition if statement:
if(条件1){ 条件1满足执行的语句 }else if(条件2){条件2满足执行的语句}... else{ 都不满足执行的语句}

Example:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
int score = 0;
cout << "请输入考试分数:" << endl;
cin >> score;
if (score > 600)
{
cout << "我考上了一本大学" << endl;
}
else if (score > 500)
{
cout << "我考上了二本大学" << endl;
}
else if (score > 400)
{
cout << "我考上了三本大学" << endl;
}
else
{
cout << "我未考上本科" << endl;
}
// 返回 0 表示程序正常结束。
return 0;
}
Run result (the following is an example when entering 10 , 20 , 30... in sequence as prompted):
请输入考试分数:
我未考上本科
Nested if statement : Within an if statement, another if statement can be nested to achieve more precise condition judgment
Case requirements:
- Prompt the user to enter a college entrance examination score, and make the following judgments based on the score.
- If the score is greater than 600, it is considered admission to a first-tier university; if greater than 500, admission to a second-tier university; if greater than 400, admission to a third-tier university; otherwise, it is considered not admitted to an undergraduate program.
- Among first-tier university scores, if greater than 700, admission to Peking University; if greater than 650, admission to Tsinghua University; if greater than 600, admission to Renmin University of China.
Example:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
int score = 0;
cout << "请输入考试分数:" << endl;
cin >> score;
if (score > 600)
{
cout << "我考上了一本大学" << endl;
if (score > 700)
{
cout << "我考上了北大" << endl;
}
else if (score > 650)
{
cout << "我考上了清华" << endl;
}
else
{
cout << "我考上了人大" << endl;
}
}
else if (score > 500)
{
cout << "我考上了二本大学" << endl;
}
else if (score > 400)
{
cout << "我考上了三本大学" << endl;
}
else
{
cout << "我未考上本科" << endl;
}
// 返回 0 表示程序正常结束。
return 0;
}
Run result (the following is an example when entering 10 , 20 , 30... in sequence as prompted):
请输入考试分数:
我未考上本科
Practice case: Three little pigs weigh themselves
There are three little pigs A, B, and C. Please input the weights of the three little pigs respectively, and determine which little pig is the heaviest?
1.1.2.Ternary operator
Purpose: Implement simple judgment via the ternary operator
Syntax:表达式1 ? 表达式2 :表达式3
Explanation:
If the value of expression 1 is true, execute expression 2 and return the result of expression 2;
If the value of expression 1 is false, execute expression 3 and return the result of expression 3.
Example:
int main() {
int a = 10;
int b = 20;
int c = 0;
c = a > b ? a : b;
cout << "c = " << c << endl;
//C++中三目运算符返回的是变量,可以继续赋值
(a > b ? a : b) = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
return 0;
}
Operation result:
c = 20
a = 10
b = 100
c = 20
Summary: Compared with the if statement, the ternary operator has the advantage of being short and clean, but the disadvantage is that if nesting is used, the structure is not clear.
1.1.3.switch statement
**Function:** Execute multi-condition branch statements
Syntax:
switch(表达式)
{
case 结果1:执行语句;break;
case 结果2:执行语句;break;
...
default:执行语句;break;
}
Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.
Example:
int main() {
//请给电影评分
//10 ~ 9 经典
// 8 ~ 7 非常好
// 6 ~ 5 一般
// 5分以下 烂片
int score = 0;
cout << "请给电影打分" << endl;
cin >> score;
switch (score)
{
case 10:
case 9:
cout << "经典" << endl;
break;
case 8:
cout << "非常好" << endl;
break;
case 7:
case 6:
cout << "一般" << endl;
break;
default:
cout << "烂片" << endl;
break;
}
return 0;
}
Run result (the following is an example when entering 10 , 20 , 30... in sequence as prompted):
请给电影打分
经典
Note 1: In a switch statement, the type of the expression can only be integer or character type.
Note 2: If there is no break in a case, the program will continue executing downward.
Summary: Compared with if statements, for multi-condition judgments, the switch structure is clear and execution efficiency is high; the disadvantage is that switch cannot judge intervals.
1.2.Loop structure
1.2.1.while loop statement
**Function:** When the loop condition is satisfied, execute the loop statement.
Syntax: while(循环条件){ 循环语句 }
Explanation:==As long as the result of the loop condition is true, execute the loop statement==

Example:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
int num = 0;
while (num < 10)
{
cout << "num = " << num << endl;
num++;
}
// 返回 0 表示程序正常结束。
return 0;
}
Operation result:
num = 0
num = 1
num = 2
num = 3
num = 4
num = 5
num = 6
num = 7
num = 8
num = 9
Note: When executing a loop statement, the program must provide an exit from the loop; otherwise, an infinite loop will occur
While loop practice example:==Guess the number==
**Case description:** The system randomly generates a number between 1 and 100; the player makes a guess. If the guess is wrong, the player is prompted that the number is too large or too small. If the guess is correct, the player is congratulated on winning, and the game exits.

1.2.2.do...while loop statement
Function: When the loop condition is satisfied, execute the loop statement.
Syntax: do{ 循环语句 } while(循环条件);
**Note:** The difference from while is that ==do...while executes the loop statement once first==, and then evaluates the loop condition.

Example:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
int num = 0;
do
{
cout << num << endl;
num++;
} while (num < 10);
// 返回 0 表示程序正常结束。
return 0;
}
Operation result:
0
1
2
3
4
5
6
7
8
9
Summary: The difference from a while loop is that do...while executes the loop body once first, and then evaluates the loop condition.
Practice Case: Narcissistic Numbers
**Case description:** A narcissistic number refers to a 3-digit number whose value equals the sum of the 3rd powers of each of its digits.
For example: 1^3 + 5^3 + 3^3 = 153
Please use a do...while statement to find all Armstrong numbers among all 3-digit numbers.
1.2.3.for loop statement
Function: When the loop condition is satisfied, execute the loop statement.
Syntax: for(起始表达式;条件表达式;末尾循环体) { 循环语句; }
Example:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
// 返回 0 表示程序正常结束。
return 0;
}
Operation result:
0
1
2
3
4
5
6
7
8
9
Detailed explanation:

Note: In a for loop, the expressions must be separated by semicolons.
Summary: while, do...while, and for are all commonly used loop statements in development; the for loop has a relatively clear structure and is more commonly used.
Practice Case: Knocking on the Table
Case description: Count from 1 to 100. If the ones digit of a number contains 7, or the tens digit contains 7, or the number is a multiple of 7, we print "knock on the table"; for all other numbers, print them directly.

1.2.4.Nested loops
Function: Nest another loop within the loop body to solve some practical problems.
For example, if we want to print the following image on the screen, we need to use nested loops.

Example:
int main() {
//外层循环执行1次,内层循环执行1轮
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << "*" << " ";
}
cout << endl;
}
return 0;
}
Operation result:
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
**Practice Case:** Multiplication Table
Case description: Use nested loops to implement the multiplication table.

1.3.Jump statement
1.3.1.break statement
Purpose: Used to break out of an ==selection structure== or a ==loop structure==
Timing for using break:
- Appears in a switch conditional statement; its effect is to terminate the case and exit the switch.
- Appears in a loop statement; its effect is to exit the current loop statement.
- Appears in a nested loop; it exits the nearest inner loop statement.
Example 1:
int main() {
//1、在switch 语句中使用break
cout << "请选择您挑战副本的难度:" << endl;
cout << "1、普通" << endl;
cout << "2、中等" << endl;
cout << "3、困难" << endl;
int num = 0;
cin >> num;
switch (num)
{
case 1:
cout << "您选择的是普通难度" << endl;
break;
case 2:
cout << "您选择的是中等难度" << endl;
break;
case 3:
cout << "您选择的是困难难度" << endl;
break;
}
return 0;
}
Run result (the following is an example when entering 10 , 20 , 30... in sequence as prompted):
请选择您挑战副本的难度:
1、普通
2、中等
3、困难
Example 2:
int main() {
//2、在循环语句中用break
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break; //跳出循环语句
}
cout << i << endl;
}
return 0;
}
Operation result:
0
1
2
3
4
Example 3:
int main() {
//在嵌套循环语句中使用break,退出内层循环
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (j == 5)
{
break;
}
cout << "*" << " ";
}
cout << endl;
}
return 0;
}
Operation result:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
1.3.2.continue statement
**Purpose:** In an ==iteration statement==, skip the remaining statements in the current iteration that have not yet been executed, and proceed to the next iteration.
Example:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
for (int i = 0; i < 100; i++)
{
if (i % 2 == 0)
{
continue;
}
cout << i << endl;
}
// 返回 0 表示程序正常结束。
return 0;
}
Operation result:
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99
Note: continue does not terminate the entire loop, whereas break will exit the loop.
1.3.3.goto statement
**Function:** Can unconditionally jump to a statement
Syntax: goto 标记;
**Explanation:** If the label name exists, when the goto statement is executed, control jumps to the position of the label
Example:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
cout << "1" << endl;
goto FLAG;
cout << "2" << endl;
cout << "3" << endl;
cout << "4" << endl;
FLAG:
cout << "5" << endl;
// 返回 0 表示程序正常结束。
return 0;
}
Operation result:
1
5
Note: In programs, it is not recommended to use goto statements, so as to avoid confusion in the program flow.