C language - loop statement_study notes

Overview of loop statements

The role of loop statements in C language:

The function of the loop statement in C language is to allow the program to repeatedly execute a section of code, thereby achieving operations such as batch processing of data or repeated calculations. C language provides a variety of loop statements, including for loop, while loop, and do-while loop. Each loop statement has its specific usage scenarios and grammatical characteristics. By using loop statements, the program can implement repetitive operations more concisely and efficiently, improving the readability and maintainability of the program.

Classification of loop statements

for loop

The for loop is usually used to iterate through an array or perform a certain number of operations on a value. The following is the syntax of the for loop:

for (初始化表达式; 条件表达式; 更新表达式)
{
    
    
    // 循环体
}

Among them, the initialization expression is used to set the initial value of the loop variable; the conditional expression is used to determine whether the loop continues to execute; the update expression is used to update the value of the loop variable.

For example, the following code uses a for loop to output numbers from 1 to 10:

for (int i = 1; i <= 10; i++)
{
    
    
    printf("%d\n", i);
}

while loop

A while loop is used to repeatedly execute a block of code while a certain condition is true. The following is the syntax of the while loop:

while (条件表达式)
{
    
    
    // 循环体
}

Among them, the conditional expression is used to determine whether the loop continues to execute.

For example, the following code uses a while loop to output numbers from 1 to 10:

int i = 1;
while (i <= 10) 
{
    
    
    printf("%d\n", i);
    i++;
}

do-while loop

The do-while loop is similar to the while loop, but the code block is executed at least once because its condition is evaluated after the code block is executed. Here is the syntax for a do-while loop:

do
{
    
    
    // 循环体
} while (条件表达式);

Among them, the conditional expression is used to determine whether the loop continues to execute.

For example, the following code uses a do-while loop to output numbers from 1 to 10:

int i = 1;
do 
{
    
    
    printf("%d\n", i);
    i++;
} while (i <= 10);

Comparison between while loop and for loop

Insert image description here
Both for and while have three parts: initialization, judgment, and adjustment in the process of implementing the loop. However, the three parts of the for loop are very concentrated, which facilitates code maintenance. If there is a lot of code, the three parts of the while loop are It is relatively scattered, so the for loop is better in form.

break and continue statements

break statement

  1. The break statement is used to jump out of the current loop (or switch statement) and continue executing the code after the loop (or switch).
  2. When using the break statement to break out of a loop, all code after the loop will be skipped, and the program will directly execute the next statement.
  3. For example, the loop in the following code will only be executed once, because the break statement is encountered during the second execution and the loop is jumped out of:
for (int i = 0; i < 10; i++) 
{
    
    
    if (i == 5) 
    {
    
    
        break;
    }
    printf("%d\n", i);
}

The output is as follows:
Insert image description here

continue statement

  1. The continue statement is used to skip the remaining code in the current loop and enter the next loop.
  2. When you use the continue statement to skip an iteration in a loop, all code in that iteration will be skipped and the next iteration will be executed directly.
  3. For example, the loop in the following code will print from 0 to 9, but when i equals 5, the continue statement will skip the remaining code and directly execute the next loop, so only the numbers 0 to 4 and 6 to 9 are printed. :
for (int i = 0; i < 10; i++) 
{
    
    
    if (i == 5) 
    {
    
    
        continue;
    }
    printf("%d\n", i);
}

The output results are as follows;
Insert image description here

nesting of loops

In the process of solving practical problems, loops are often nested together to solve the problem better. This is what we call: loop nesting.
For example:

int main() 
{
    
    
    for (int i = 1; i <= 10; i++) 
    {
    
    
        for (int j = 1; j <= i; j++)
        {
    
    
            printf("%d*%d=%d\t", j, i, i * j);
        }
        printf("\n");
    }
    return 0;
}

The above code is to print the multiplication table from 1 to 10. The output result is as follows:
Insert image description here
In this example, the outer loop controls the number of rows and the inner loop controls the number of columns. The inner loop will output a multiplication expression every time it is executed, and finally the outer loop will output a newline character so that the multiplication expression of each line can be displayed separately.

Guess you like

Origin blog.csdn.net/yjagks/article/details/131976632