Do – while loop
The general syntax of a do-while loop is as follows:
do {
// code to execute
} while (condition);
The condition can be any Boolean expression. The do-while loop executes the code block at least once, and then it evaluates the condition. If the condition is true, the code block is executed again. The code block is executed repeatedly until the condition evaluates to false.
Here is an example of a do-while loop:
#include <iostream>
using namespace std;
int main()
{
int c = 0, num = 0, sum = 0;
do
{
cout << "Enter a number: ";
cin >> num;
sum += num;
c++;
}
while (c <= 4);
cout << "sum = " << sum << endl;
return 0;
}
This code prompts the user to enter four numbers and then prints the sum of those numbers to the console. The program uses a do-while loop to ensure that the user enters at least one number, even if they enter an invalid number the first time.
Here is a step-by-step explanation of what happens when you run this code:
- The compiler declares the
c,num, andsumvariables and initializes them to the values 0, 0, and 0, respectively. - The
dostatement begins the do-while loop. - The
cout << "Enter a number: ";statement prints the message “Enter a number: ” to the console. - The
cin >> num;statement reads a number from the user and stores it in thenumvariable. - The
sum += num;statement increments the value of thesumvariable by the value of thenumvariable. - The
c++;statement increments the value of thecvariable by 1. - The
while (c <= 4);statement evaluates the condition of the do-while loop. If thecvariable is less than or equal to 4, the loop will continue to iterate. Otherwise, the loop will terminate. - If the condition of the loop is true, the code block inside the loop is executed.
- Steps 3-7 are repeated until the condition of the loop is false.
- After the loop has terminated, the program prints the message “sum = ” to the console, followed by the value of the
sumvariable.
Here is an example of the output of this program:
Enter a number: 10 Enter a number: 20 Enter a number: 30 Enter a number: 40 sum = 100
Do-while loops can be used to solve a variety of problems, such as:
- Validating user input
- Checking for a certain condition to be met
- Performing a task a certain number of times
The differences between while loop and do – while loop:
| Feature | While loop | Do-while loop |
|---|---|---|
| Condition is checked | Before the code block is executed. | After the code block is executed. |
| Guarantee of execution | The code block is not guaranteed to be executed at least once. | The code block is guaranteed to be executed at least once. |
Table explanation
- While loop: The while loop checks the condition before the code block is executed. If the condition is true, the code block is executed. The code block is executed repeatedly until the condition evaluates to false.
- Do-while loop: The do-while loop checks the condition after the code block is executed. The code block is executed at least once, regardless of the value of the condition. The code block is executed repeatedly until the condition evaluates to false.