Home > Article > Backend Development > What is the usage of do while statement in C language?
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.
The general form of the do-while statement is:
do{ 代码语句 } while(表达式);
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; }
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!