The do-while
loop in C++ is similar to the while
loop, but with one important difference: the condition is checked after the loop's code has been executed. This means the loop will always execute at least once, even if the condition is false
initially.
1️⃣ Syntax of do-while
Loop:
do {
// Code to be executed
} while (condition);
- The code inside the
do
block runs at least once, regardless of the condition. - After executing the code inside the
do
block, the condition is checked. - If the condition is true, the loop continues; otherwise, it terminates.
2️⃣ Example: Basic do-while
Loop
Here's a simple example that prints numbers from 1 to 5 using a do-while
loop:
#include <iostream>
using namespace std;
int main() {
int i = 1;
// do-while loop to print numbers 1 to 5
do {
cout << i << " ";
i++; // Incrementing i
} while (i <= 5);
return 0;
}
Explanation:
- Initialization:
i
starts at 1. - Action: The current value of
i
is printed, andi
is incremented by 1. - Condition: The loop continues as long as
i <= 5
.
Output:
1 2 3 4 5
3️⃣ Example: Using do-while
with User Input
This example asks the user to input numbers and continues until they enter 0
:
#include <iostream>
using namespace std;
int main() {
int num;
do {
cout << "Enter a number (0 to stop): ";
cin >> num;
cout << "You entered: " << num << endl;
} while (num != 0);
cout << "Program terminated." << endl;
return 0;
}
Explanation:
- The loop will always prompt the user for input at least once.
- If the user enters
0
, the loop will terminate and print"Program terminated."
- If any other number is entered, it will keep asking for more numbers until
0
is entered.
Output:
Enter a number (0 to stop): 3
You entered: 3
Enter a number (0 to stop): 5
You entered: 5
Enter a number (0 to stop): 0
Program terminated.
4️⃣ Difference Between while
and do-while
Loops:
while
loop: The condition is checked before the code inside the loop is executed. If the condition isfalse
initially, the code inside the loop will never execute.do-while
loop: The code inside the loop is executed at least once before the condition is checked.
5️⃣ Common Use Cases for do-while
Loops:
- Menu-driven programs: Where the user is prompted with a menu of options and must choose one. This is useful for situations where you want to show the menu at least once before checking if the user wants to exit.
- Input validation: When you want to keep asking the user for input until they provide a valid answer (e.g., a positive number).
- Interactive programs: Asking questions or taking actions repeatedly until a user gives a valid response.
6️⃣ Example: Nested do-while
Loop
You can also nest do-while
loops, where one do-while
loop is inside another. Here's an example where the user is prompted to enter multiple numbers in a loop:
#include <iostream>
using namespace std;
int main() {
int rows, number;
cout << "Enter the number of rows: ";
cin >> rows;
do {
cout << "Enter a number: ";
cin >> number;
do {
cout << number << " ";
number--;
} while (number > 0);
cout << endl;
rows--;
} while (rows > 0);
return 0;
}
Explanation:
- The outer
do-while
loop controls how many rows the user wants to enter. - The inner
do-while
loop prints the numbers in decreasing order from the number the user enters.
Output:
Enter the number of rows: 2
Enter a number: 5
5 4 3 2 1
Enter a number: 3
3 2 1
7️⃣ Key Points to Remember:
- Always runs at least once: The
do-while
loop guarantees at least one iteration, regardless of the condition. - Use when: You want the code to execute before checking the condition, like asking the user for input and validating it afterward.
- Termination: Ensure the condition eventually becomes
false
to avoid an infinite loop.
Would you like to explore more complex use cases or perhaps try combining loops with other control structures like if
statements?
0 Comments