Do While Loops
COS1511 - Introduction To Programming I · Learning C++
Do While Loops
A do while loop is a control structure in C++ that allows you to execute a block of code at least once, and then repeat the execution as long as a specified condition is true. This is different from a while loop, which evaluates the condition before executing the block of code.
Syntax of a Do While Loop
The syntax of a do while loop is as follows:
do {
// block of code to be executed
} while (condition);Here, the code inside the braces will execute first, and then the condition will be checked. If the condition is true, the code will execute again. If the condition is false, the loop will terminate.
Example of a Do While Loop
Let’s look at a simple example of a do while loop. This program will prompt the user to enter a number until they enter a number greater than 10.
#include <iostream>
using namespace std;
int main() {
int number;
do {
cout << "Enter a number (greater than 10 to stop): ";
cin >> number;
} while (number <= 10);
cout << "You entered a number greater than 10: " << number << endl;
return 0;
}In this code:
- The program prompts the user to enter a number.
- The do while loop checks if the number is less than or equal to 10.
- If the condition is true, the loop continues, prompting the user again.
- If the user enters a number greater than 10, the loop stops and displays the message.
Remember: The code inside the do block will always execute at least once, regardless of the condition.
Using Do While Loops for Input Validation
Do while loops are useful for input validation. For example, if you want to ensure that a user enters a valid age, you can use a do while loop to keep asking for input until a valid age is entered.
#include <iostream>
using namespace std;
int main() {
int age;
do {
cout << "Enter your age (0-120): ";
cin >> age;
} while (age < 0 || age > 120);
cout << "Your age is: " << age << endl;
return 0;
}In this example:
- The user is prompted to enter their age.
- The loop continues until the user enters a valid age between 0 and 120.
- The condition uses the logical OR operator (||) to check for invalid input.
Watch out: Be careful with the condition in a do while loop. If the condition is always true, it can create an infinite loop, which will cause your program to run indefinitely.
Nested Do While Loops
You can also use nested do while loops. A nested loop is a loop inside another loop. Here is an example of how to use a nested do while loop to create a simple multiplication table:
#include <iostream>
using namespace std;
int main() {
int i = 1, j;
do {
j = 1;
do {
cout << i << " x " << j << " = " << (i * j) << endl;
j++;
} while (j <= 10);
i++;
} while (i <= 10);
return 0;
}In this code:
- The outer do while loop controls the first number (i) from 1 to 10.
- The inner do while loop controls the second number (j) from 1 to 10.
- For each value of i, the program calculates the product of i and j and displays it.
Summary
- A do while loop executes the block of code at least once.
- The condition is checked after the code execution.
- It is useful for input validation and can be nested.
Check your understanding
- What is the main difference between a while loop and a do while loop?
- Write a do while loop that prompts the user for a password until they enter the correct one.
- Explain how you would prevent an infinite loop in a do while structure.
- Provide an example of a situation where a do while loop would be more appropriate than a for loop.