Latest web development tutorials

C do ... while loop

C cycle C cycle

Unlike thefor and whileloops, they are testing the loop condition of the loop. In the Clanguage, do ... while loop is to check its condition at the end of the loop.

do ... while loop is similar to the while loop, but do ... while loop will ensure the implementation of at least a cycle.

grammar

C language syntaxdo ... while loop:

do
{
   statement (s);

} While (condition);

Please note that the conditional expressions appear at the end of the loop, so the loop statement (s) will be executed at least once before the condition is tested.

If the condition is true, the flow of control jumps back above do, and then re-execute the loop statement (s). This process is repeated until the given condition becomes false so far.

flow chart

C in the do ... while loop

Examples

#include <stdio.h>
 
int main ()
{
   /* 局部变量定义 */
   int a = 10;

   /* do 循环执行 */
   do
   {
       printf("a 的值: %d\n", a);
       a = a + 1;
   }while( a < 20 );
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
a 的值: 15
a 的值: 16
a 的值: 17
a 的值: 18
a 的值: 19

C cycle C cycle