Introduction to C language do while
In addition to the while statement, the C language also provides do...while statements to implement loops.
general form
do
statement
while (expression)
The statement is the loop body. First execute the specified loop statement once, and then judge the expression. When the value of the expression is non-zero (true), return to re-execute the loop body statement, and so on until the value of the expression is equal to 0 ( false), at which point the loop ends.
Things to note when doing do while in C language
The execution process of the C language do...while statement is to execute the loop body first, and then check whether the condition is true. If it is true, then execute the loop body.
C language uses do while statement to find the sum of 1+2+3+...+10
#include<stdio.h>//Header file
int main()//Main function
{ int i=1,sum=0;//Define variables do{ sum=sum+i; i=i+1; }while( i<11); printf("%d",sum);//output result return 0;//function return value is 0 }
Compile and run results:
55
--------------------------------
Process exited after 0.09865 seconds with return value 0
Please press any key to continue. . .