Home > Backend Development > C#.Net Tutorial > What are the three basic statements used to implement loop structures in C language

What are the three basic statements used to implement loop structures in C language

青灯夜游
Release: 2022-12-30 11:13:05
Original
22429 people have browsed it

Three basic loop statements: 1. while loop statement, syntax "while(expression){statement block}"; 2. do-while loop statement, syntax "do{statement block}while(expression );"; 3. For loop statement, the syntax is "for (initialization statement; loop condition; self-increment or self-decrement) {statement block}".

What are the three basic statements used to implement loop structures in C language

The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.

The three basic statements used to implement loop structures in the C language are: while loop statements, do-while loop statements, and for loop statements.

Tutorial recommendation: "c language tutorial video"

while loop statement

The general form of while loop is:

while(表达式){
    语句块
}
Copy after login

means, first calculate the value of "expression", when the value is true (non-0), execute the "statement block"; after executing the "statement block", calculate the value of the expression again, if True, continue to execute the "statement block"... This process will be repeated until the value of the expression is false (0), then exit the loop and execute the code after the while.

We usually call the "expression" a loop condition and the "statement block" a loop body. The entire loop process is the process of constantly judging the loop condition and executing the loop body code.

Use while loop to calculate the value of 1 added to 100:

#include <stdio.h>
int main(){
    int i=1, sum=0;
    while(i<=100){
        sum+=i;
        i++;
    }
    printf("%d\n",sum);
    return 0;
}
Copy after login

Run result:

5050
Copy after login
Copy after login
Copy after login

Code analysis:

1) When the program runs to while , because i=1 and i<=100 are true, so the loop body will be executed; after the execution, the value of i becomes 2 and the value of sum becomes 1.

2) Next, we will continue to judge whether i<=100 is true, because at this time i=2, i<=100 is true, so we continue to execute the loop body; after the execution, the value of i becomes 3, sum The value becomes 3.

3) Repeat step 2).

4) When the loop reaches the 100th time, the value of i changes to 101 and the value of sum changes to 5050; because i<=100 is no longer true at this time, the loop is exited and the loop is no longer executed. body, and then execute the code behind the while loop.

The overall idea of ​​the while loop is as follows: Set a loop condition with variables, that is, an expression with variables; add an additional statement to the loop body so that it can change the loop condition The value of the variable. In this way, as the loop continues to execute, the values ​​of the variables in the loop condition will continue to change. There will eventually be a moment when the loop condition is no longer true and the entire loop ends.

What happens if the loop condition does not contain variables?

1) If the loop condition is true, the while loop will continue to execute and never end, becoming an "infinite loop".

2) If the loop condition is not true, the while loop will not be executed even once.

do-while loop

In addition to the while loop, there is also a do-while loop in C language.

The general form of the do-while loop is:

do{
    语句块
}while(表达式);
Copy after login

The difference between the do-while loop and the while loop is that it will first execute the "statement block" and then determine whether the expression is true. , if true, continue the loop; if false, terminate the loop. Therefore, the do-while loop must execute the "block" at least once.

Use do-while to calculate the value of 1 added to 100:

#include <stdio.h>
int main(){
    int i=1, sum=0;
    do{
        sum+=i;
        i++;
    }while(i<=100);
    printf("%d\n", sum);
    return 0;
}
Copy after login

Running result:

5050
Copy after login
Copy after login
Copy after login

Notewhile(i<= 100);The final semicolon; is a must.

for loop statement

In addition to the while loop, there is also a for loop in C language. Its use is more flexible and can completely replace the while loop.

The general form of the for loop is:

for(表达式1; 表达式2; 表达式3){
    语句块
}
Copy after login

Its running process is:

1) First execute "expression 1"--"initialization statement".

2) Then execute "Expression 2"--"Loop Condition"; if its value is true (non-0), execute the loop body, otherwise end the loop.

3) After executing the loop body, execute "expression 3" -- "self-increment or self-decrement".

4) Repeat steps 2) and 3) until the value of "expression 2" is false, then end the loop.

In the above steps, 2) and 3) are a loop and will be executed repeatedly. The main function of the for statement is to continuously execute steps 2) and 3).

"Expression 1" is only executed during the first loop and will not be executed again in the future. This can be considered an initialization statement. "Expression 2" is generally a relational expression, which determines whether to continue the next loop, which is called the "loop condition". "Expression 3" is often an expression with an increment or decrement operation, so that the loop condition gradually becomes "not true".

The execution process of the for loop can be represented by the following figure:

What are the three basic statements used to implement loop structures in C language

Use the for statement to calculate the value of 1 added to 100:

#include <stdio.h>
int main(){
    int i, sum=0;
    for(i=1; i<=100; i++){
        sum+=i;
    }
    printf("%d\n",sum);
    return 0;
}
Copy after login

Run Result:

5050
Copy after login
Copy after login
Copy after login

Code analysis:

1) When executing the for statement, first assign an initial value of 1 to i and determine whether i

2) During the second loop, the value of i is 2, i

3) Repeat step 2) until the 101st loop. At this time, the value of i is 101, and i

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of What are the three basic statements used to implement loop structures in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template