Pages

C++ do-while Loop

A do-while loop acts just like a while loop, except the condition is checked at the end of each pass through the loop body. This means a do-while loop will execute at least once.

Flow of do-while Loop in C++ by Flow Diagram


C++ do while Loop Flowchart diagram
C++ do-while Loop Flowchart Diagram

Syntax:

    do {
        // loop body
    } while ( condition );

Example:

#include <iostream>
using namespace std;
 int main () {
   // do while loop example
   int i=0;
   do
  {
      cout << "Apple: " << i << endl; // output
      i++; // increment counter
   }
   while(i < 5); // condition
   return 0;

}

Output: