Home>Article>Backend Development> What are the structured statements that can be used to implement loops in C language?

What are the structured statements that can be used to implement loops in C language?

青灯夜游
青灯夜游 Original
2021-02-01 17:02:42 6144browse

The structured statements that C language can use to implement loops are: while, do-while and for statements. The while statement is a judgment condition. If it is met, it will be executed, otherwise the loop will end; the do-while statement will be executed first, and then it will be judged whether the condition is met; the for statement allows you to write a loop control structure that executes a specified number of times.

What are the structured statements that can be used to implement loops in C language?

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

In C language, loop structure: execute the same code repeatedly.

for loop

The general form of for loop:

for(初始化语句; 循环条件; 自增或自减){ 语句块 }

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

What are the structured statements that can be used to implement loops in C language?

Example: Code for "Calculating the sum from 1 to 100":

#include  int main(){ int i, sum=0; for(i=1; i<=100; i++){ sum+=i; } printf("%d\n",sum); return 0; }

Running result:

5050

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

Tutorial recommendation: "c language tutorial video"

while loop

The general form of while loop is :

while(表达式){ 语句块 }

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 it is 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.

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

#include  int main(){ int i=1, sum=0; while(i<=100){ sum+=i; i++; } printf("%d\n",sum); return 0; }

Running result:

5050

Code analysis:

1) When the program runs to while, because i=1 and i

2) Next, we will continue to judge whether i

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

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.

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(表达式);

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.

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

#include  int main(){ int i=1, sum=0; do{ sum+=i; i++; }while(i<=100); printf("%d\n", sum); return 0; }

Run result:

5050

Note:while(iThe final semicolon;is a must.

For more knowledge about computer programming, please visit:Programming Video! !

The above is the detailed content of What are the structured statements that can be used to implement loops in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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