Do While Loop in C++

Do While Loop in C++ programming will execute the code block once, before checking if the condition is true, and repeat a specific block of code until the condition is met.

General form of Do While Loop


do
{
this;
and this;
and this;
and this;
}
while(this condition is true);

Program Printing 0 to 10 using Do While Loop

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int a = 0;
do //doing then Statement.
{
cout<<a<<endl;
a++; //adding 1 to 'a'.
}
while(a <= 10); //loop looping from 0 to 10.
getch();
}
Output
0
1
2
3
4
5
6
7
8
9
10

Program Password using Do While Loop

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int val;
do //executing process.
{
cout<<"Please Enter Password: ";
cin>>val; //user enter value storing in 'val'.
}
while(val != 54265); //loop stops when 'val' value Equal to 54265.
cout<<"Password Correct!";
getch();
}
Output
Please Enter Password: 3433
Please Enter Password: 225
Please Enter Password: 1234
Please Enter Password: 54321
Please Enter Password: 54265
Password Correct!