program flow structure
program flow structure
C/C++ supports three basic program structures: ==sequential structure, selection structure, loop structure==
- Sequential structure: The program executes in order, without any jumps.
- Selection structure: Based on whether conditions are met, it selectively executes corresponding functions.
- Loop structure: Execute a block of code repeatedly based on whether a condition is met.
Selection Structure
if statement
Purpose: Execute statements that meet the conditions
Three forms of if statements
- Single-line format if statement
- Multi-line format if statement
- multi-condition if statements
- 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;
}
Note: Do not place a semicolon after if condition expressions.
- 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/Observe Results: During runtime, you need to input data as prompted, and the output will vary based on the input content and branch conditions.
- Multi-condition if statements:
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/Observe Results: During runtime, you need to input data as prompted, and the output will vary based on the input content and branch conditions.
Nested if statements: Within an if statement, you can nest another if statement to achieve more precise conditional judgment.
Case requirements:
- Prompt the user to enter a college entrance exam score, and make the following judgments based on the score:
- If the score is greater than 600 points, it is considered as admitted to a first-tier university; greater than 500 points, admitted to a second-tier university; greater than 400 points, admitted to a third-tier university; the rest are considered as not admitted to undergraduate programs;
- In a particular scoring system, a score above 700 leads to admission to Peking University, above 650 to Tsinghua University, and above 600 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/Observe Results: During runtime, you need to input data as prompted, and the output will vary based on the input content and branch conditions.
Practice Case: Three Little Pigs Weighing Themselves
There are three little pigs, A, B, and C. Please input the weight of each pig and determine which one is the heaviest?
ternary operator
Function: Implement simple conditionals using 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;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
Summary: Compared to if statements, the ternary operator's advantage lies in its concise and clean structure, while its disadvantage is that nested usage can lead to unclear code structure.
switch statement
Function: Executes multi-conditional branch statements
Syntax:
switch(表达式)
{
case 结果1:执行语句;break;
case 结果2:执行语句;break;
...
default:执行语句;break;
}
Run/Observation Results: This is a syntax fragment, focus on the writing style; run it after completing the context.
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/Observe Results: During runtime, you need to input data as prompted, and the output will vary based on the input content and branch conditions.
Note 1: The expression in a switch statement must be of integer or character type.
Note 2: If there is no break in the case, the program will continue to execute downwards.
Summary: Compared to if statements, switch statements are more structurally clear and efficient in execution when handling multiple conditions, though they cannot evaluate ranges.
Loop Structure
while循环语句是一种控制流语句,它允许根据指定的布尔条件重复执行一段代码。其基本结构包括一个条件表达式和一个循环体。只要条件为真,循环体内的代码就会不断执行。当条件变为假时,循环终止,程序继续执行后续代码。
Function: Meets the loop condition to execute the loop statements.
Syntax: while(循环条件){ 循环语句 }
Explanation: ==As long as the loop condition evaluates to true, the loop statement is executed==

Example:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
int num = 0;
while (num < 10)
{
cout << "num = " << num << endl;
num++;
}
// 返回 0 表示程序正常结束。
return 0;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
Note: When executing loop statements, the program must provide an exit to break out of the loop; otherwise, an infinite loop will occur.
while loop practice example: ==Guess the Number==
Example Description: The system randomly generates a number between 1 and 100. The player guesses the number. If the guess is incorrect, the system indicates whether the number is too high or too low. If the guess is correct, the player is congratulated on winning, and the game exits.

do...while loop statement
Purpose: Satisfy the loop condition, executes the loop statement.
Syntax: do{ 循环语句 } while(循环条件);
Note: The key difference from while is that ==do...while executes the loop statement once first==, then evaluates the loop condition

Example:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
int num = 0;
do
{
cout << num << endl;
num++;
} while (num < 10);
// 返回 0 表示程序正常结束。
return 0;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
In summary: The difference from a while loop is that a do...while loop executes the loop body once first, then evaluates the loop condition.
Exercise Case: Narcissistic Number
Case Description: A narcissistic number is a 3-digit number where the sum of the cubes of its digits equals the number itself.
For example: 1^3 + 5^3 + 3^3 equals 153.
Please use a do...while loop to find all three-digit narcissistic numbers.
for loop statement
Purpose: Satisfy the loop condition, executes the loop statement.
Syntax: for(起始表达式;条件表达式;末尾循环体) { 循环语句; }
Example:
int main() {
// 程序从 main 函数开始执行,下面的语句会按顺序运行。
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
// 返回 0 表示程序正常结束。
return 0;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
Detailed explanation:

Note: Use semicolons to separate expressions in the for loop.
In summary, while, do...while, and for loops are all commonly used in development; the for loop has a clearer structure and is particularly prevalent.
Practice Exercise: Knocking on a Table
Problem Description: Count from 1 to 100. If the units digit of the number is 7, or the tens digit is 7, or the number is a multiple of 7, we print "knock on the table". For all other numbers, print the number directly.

Nested loops
Function: Adding a nested loop within a loop to solve 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;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
Practice Example: Multiplication Table
Example description: Using nested loops to implement a multiplication table.

Jump statements
break statement
Purpose: Used to exit a ==selection structure== or a ==loop structure==
Timing for using break:
- The break statement, used within a switch conditional statement, terminates the current case and exits the switch block.
- It appears within loop constructs, serving to terminate the current loop.
- appears in nested loops, break out of the innermost 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/Observe Results: During runtime, you need to input data as prompted, and the output will vary based on the input content and branch conditions.
Example 2:
int main() {
//2、在循环语句中用break
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break; //跳出循环语句
}
cout << i << endl;
}
return 0;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
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;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
The continue statement is used in programming to skip the current iteration of a loop and proceed directly to the next iteration. When encountered, it immediately jumps to the loop's condition check or next iteration, bypassing any remaining code in the loop body for that cycle. This is useful for handling specific conditions without terminating the entire loop.
Purpose: In a ==loop statement==, it skips the remaining unexecuted statements in the current iteration and proceeds to execute 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;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
Note: continue does not terminate the entire loop, but break will exit the loop.
goto statement
Function: An unconditional jump statement
Syntax: goto 标记;
Explanation: When a label exists, encountering a goto statement causes execution to jump to the position of that 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;
}
Running/Observation Results: After running, the corresponding content will be printed according to the output statements. The variable values can be inferred based on the order of initialization, assignment, and function calls.
Note: It is not recommended to use goto statements in the program to avoid causing confusion in the program flow.