Home > Backend Development > C#.Net Tutorial > What is the usage of do while statement in C language?

What is the usage of do while statement in C language?

烟雨青岚
Release: 2020-07-06 16:37:30
Original
13873 people have browsed it

The "do while" statement in C language is used for statement loop judgment; the syntax is: "do {code statement} while (expression);". The difference between it and the while loop is that "do while" first executes the statements in the loop, and then determines whether the expression is true. If it is true, the loop continues; if it is false, the loop is terminated.

What is the usage of do while statement in C language?

The general form of the do-while statement is:

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

This loop is the same as while The difference in a loop is that it first executes the statements in the loop, and then determines whether the expression is true. If it is true, it continues the loop; if it is false, it terminates the loop. Therefore, the do-while loop must execute the loop statement at least once. Its execution process can be represented by the figure below.

[Example 6-5] Use do-while statement to calculate the value from 1 to 100

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

Similarly when there are many statements participating in the loop, use "{" and "}" Enclose them.

Recommended tutorial: "C Language"

The above is the detailed content of What is the usage of do while statement 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