Latest web development tutorials

C ++ do ... while loop

C ++ loop C ++ loop

Unlike thefor and whileloops, they are testing the loop condition of the loop.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 ++,do ... while loop syntax:

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

In C ++ do ... while loop

Examples

#include <iostream>
using namespace std;
 
int main ()
{
   // 局部变量声明
   int a = 10;

   // do 循环执行
   do
   {
       cout << "a 的值:" << a << endl;
       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 ++ loop C ++ loop