do-while
Loops are fundamental in programming as they allow you to repeat a block of code multiple times. In C++, the do-while loop is a control flow statement that executes a block of code at least once and then repeatedly executes it as long as a given condition is true. This loop is unique because the condition is checked after the loop body has been executed, ensuring that the code inside the loop runs at least once, regardless of the condition.
The Structure of a do-while Loop
The syntax of a do-while loop in C++ is as follows:
- do: This keyword indicates the start of the loop body.
- condition: This is a boolean expression that is evaluated after each iteration of the loop. If the condition is true, the loop body will execute again; if false, the loop will terminate.
- while: The loop will continue as long as the condition is true.
Key Characteristics of the do-while Loop
- Guaranteed Execution: The loop body is always executed at least once, even if the condition is false on the first check.
- Post-Condition Check: The condition is evaluated after the loop body has been executed, not before.
Example 1: Basic do-while Loop
Let’s look at a simple example where a do-while loop is used to print numbers from 1 to 5.
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << i << endl;
i++;
} while (i <= 5);
return 0;
}
Explanation:
- Here, the loop starts with i equal to 1.
- The loop body prints the value of i and then increments it.
- The condition i <= 5 is checked after the loop body has executed. The loop continues as long as this condition is true.
- When i becomes 6, the condition is false, and the loop terminates.
Example 2: Using a do-while Loop for Input Validation
A common use case for a do-while loop is input validation, where you need to ensure that a user provides a valid input.
#include <iostream>
using namespace std;
int main() {
int number;
do {
cout << "Enter a number between 1 and 10: ";
cin >> number;
} while (number < 1 || number > 10);
cout << "You entered: " << number << endl;
return 0;
}
Explanation:
- The loop prompts the user to enter a number between 1 and 10.
- If the user enters a number outside this range, the loop repeats, asking for input again.
- The loop only exits when the user enters a valid number.
Example 3: Using do-while with a Menu
A do-while loop can be used to create a menu-driven program that repeatedly presents options to the user until they choose to exit.
#include <iostream>
using namespace std;
int main() {
int choice;
do {
cout << "Menu:" << endl;
cout << "1. Option 1" << endl;
cout << "2. Option 2" << endl;
cout << "3. Option 3" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "You selected Option 1" << endl;
break;
case 2:
cout << "You selected Option 2" << endl;
break;
case 3:
cout << "You selected Option 3" << endl;
break;
case 4:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice, please try again." << endl;
}
} while (choice != 4);
return 0;
}
Explanation:
- The menu is displayed to the user, and they are prompted to make a choice.
- Based on the user's input, different actions are performed.
- The loop continues to present the menu until the user selects the option to exit (choice == 4).
When to Use a do-while Loop
- Input validation: When you need the user to input something and want to ensure that the input is valid.
- Menus: When you need to present a menu to the user that should be shown repeatedly until they make a valid selection or choose to exit.
- Guaranteed execution: When you want the loop body to execute at least once, regardless of the condition.
Comparison with Other Loops
- while loop: The condition is checked before the loop body is executed, meaning the body may not execute at all if the condition is false initially.
- for loop: Typically used for iterating a known number of times, with initialization, condition, and increment/decrement in one line.
Summary
- The do-while loop is ideal when you need to ensure that the loop body executes at least once.
- It checks the condition after the loop body has been executed.
- Common use cases include input validation and creating menu-driven programs.
Practice Problems
- Write a program that repeatedly asks the user to enter a positive number. The program should continue asking until the user enters a positive number.
- Create a menu-driven program using a do-while loop that allows the user to perform basic arithmetic operations (addition, subtraction, multiplication, division) until they choose to exit.
- Write a program that uses a do-while loop to print the multiplication table of a number entered by the user. The loop should repeat for different numbers until the user decides to stop.
This content provides a detailed explanation of the do-while loop in C++, including examples, when to use it, and practice problems to reinforce the understanding of this loop structure.