C++ Do-While Loop

C++ Do-while loop: In this loop the execution is terminated based upon the test condition. These are similar to while loops. We can also say that this loop is an exit-controlled loop. In the do-while loop, the program will execute the body of the loop first and then at the end of the loop. The important thing to keep in mind regarding this loop is that the loop body will be executed at least once irrespective of the test condition.

C++ Do-While loop – Syntax with Examples

Syntax:

initialization expression;
do
{
// statements
update_expression;
} while (test_expression);

c++ do while

Example 1 of C++ Do-While Loop:

#include <iostream> 
using namespace std; 
int main() 
{ 
  int i=2;//Initialization expression 
  do
  { 
     //loop body 
   cout<<"Hey Freshersnow\n"; 
     //update expression 
    i++; 
  } while (i < 1);//test expression 
    return 0; 
} 

Output:

Hey Freshersnow

Example 2 of C++ Do-While Loop:

#include<iostream>
using namespace std;
int main ()
{
  /*local variable Initialization */   
int n = 1,times=0;
/*do-while loops execution */   
do
  {
    cout<<"Billgates was a famous person:"<<n<<endl;
    n++;
  } while( n <= times );
   return 0;
}

Output:

Billgates was a famous person: 1