C++ loop body knowledge points excerpt

while loop

The while loop statement is shown as:

while (<条件表达式>)
    <语句>

Among them, while is the keyword of C and C++, <conditional expression> is the loop control condition, and the statement after <conditional expression> is the loop body.

While statement execution process

First calculate the value of <conditional expression>, if the value of <conditional expression> is 0 (ie false), skip the loop body <statement>, and execute the subsequent statements of the entire while statement;
if the value of <conditional expression> If it is non-zero (ie true), the specified <statement> will be executed. After the statement is executed, the value of <conditional expression> will be calculated. If the value of <conditional expression> is still non-zero, the specified <statement> will continue to be executed <statement>, test again... until the value of <conditional expression> is 0, skip the loop body <statement>, end the execution of the entire while statement, and then execute the subsequent statements of the entire while statement.

The execution flow chart is as follows:
Insert picture description here
For example, the following program can calculate the time (accurate to the second) for a falling iron ball on an airplane to land. Assuming that the altitude of the aircraft is 2500 meters, the relationship between the falling height d (meters) and the time t (seconds) is d=(1/2)gt*t, where g=9.8m/s^2:

// 初始状态时间为秒,下落高度为米
int sec = 0;
float dis = 0;
// 如果下落高度小于2500米,则继续循环
while (dis < 2500)
{
    
    
    // 每次循环计算每秒后的下落高度
    sec += 1;
    dis = 0.5 * 9.82 * sec * sec;
}
// 输出落地需要的秒数
cout << sec << "秒" << endl;

do-while loop

The general format is:

do <语句>
while (<条件表达式>);

Among them, do and while are both C and C++ keywords, the statement between do and while is the loop body, <conditional expression> is the loop control condition, and the end of the do-while statement is a semicolon as the end of the statement .
The loop formed by the do-while statement is different from the loop formed by the while statement: it first executes the <statement> in the loop, and then calculates the value of the <conditional expression> to determine whether the condition is true or false, and if it is true, the loop continues ; If it is false, the loop is terminated and the subsequent statements of the entire do-while statement continue to be executed.
Therefore, the do-while statement is an exit-controlled loop structure, and its loop body must be executed at least once, while the while statement is an entry-controlled loop structure, and its loop body may not necessarily be executed .
The execution flow chart of the do-while statement is as follows:
Insert picture description here
Similarly, when the loop body contains multiple statements, you can enclose them with curly braces to form a compound statement.

for loop

The general form is:

for (<初始化语句> ;[<条件表达式>]; [<增量表达式>] )
    <语句>

for is a keyword of C and C++, which indicates the beginning of a for loop statement. <Statement> is the loop body of the for statement.
The <initialization statement> can be any legal statement, and the <conditional expression> and <incremental expression> can be any legal expression. The specific instructions are as follows:
1. The initialization statement
<initialization statement> is usually an assignment statement , Used to assign initial values ​​to loop control variables. The <initialization statement> can be an expression statement or a declaration statement and ends with ";".
2. Conditional expression
<conditional expression> is an expression that can be converted into a logical value. It determines when to exit the loop. The expression can be empty (the logical value is always true when it is empty). Use ";" to separate between <conditional expression> and <incremental expression>.
3. Incremental expression
<incremental expression> defines how the loop control variable changes after each loop. The expression can also be empty, and no calculation effect will be produced at this time.

for statement execution process

First calculate the <initialization statement>, then calculate the value of the <conditional expression>.
If the value is false, the loop is ended, the <statement> of the loop body is skipped, and the subsequent statements of the entire for statement continue to execute;
if the value is true, the <statement> of the loop body is executed, and after the loop body is executed , Then execute <incremental expression>, and then calculate the value of <conditional expression>, if the value is true, execute <statement> of the loop body, then execute <incremental expression>, and then calculate <conditional expression Formula> to test, ... until the value of <conditional expression> is false, the loop is ended, the <statement> of the loop body is skipped, and the subsequent statements of the entire for statement continue to be executed.
The execution flow chart of the for statement is as follows:
Insert picture description here
For example, the following program can calculate the sum of all integers between 1 and 100 and output:

int sum = 0;     // 声明求和变量并初始化
// 求和
for (int i = 1; i <= 100; i++)
    sum += i;
cout << "The sum of 1 to 100 is: " << sum << endl;

Guess you like

Origin blog.csdn.net/interestingddd/article/details/113842805
Recommended