C++ → Basics → Input → Arithmetic → Conditions → Loops → for loop → while loop → The do-while loop
The do – while loop in C++. Construction and Examples. The do – while loop is used when we have to run a program until a given condition is true.
Construction: The do – while loop
do
{
Statement(s)
}
while (condition);
Q 1. Write a program in C++ using do – while loop, that take a number, and increase it 2 by 2, and ends before 30.
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Enter a Number: " << endl;
cin >> a;
do
{
a = a+2;
cout << a << endl;
}
while (a<28);
}
In while condition we we set a<28 so that it must not reach 30. Since it is adding 2 (a=a+2).
NEXT: Array