C++ do-while loop
The do-while
loop in C++ is a control flow statement that executes a block of code at least once and then repeatedly executes the code block as long as a specified condition is true. This is particularly useful in scenarios where you want to ensure that the code block runs at least once, such as when prompting a user for input.
Syntax of do-while
Loop
do {
// Code to be executed
} while (condition);
condition
: This is evaluated after the loop body has executed. If it istrue
, the loop will execute again; if it isfalse
, the loop will terminate.
Key Characteristics
- Guaranteed Execution: The code inside the
do
block is executed at least once, even if the condition isfalse
on the first check. - Condition Check After Execution: Unlike the
while
loop, where the condition is checked before any execution, thedo-while
loop checks the condition after executing the loop body.
Example: Basic do-while
Loop
Here’s a simple example that uses a do-while
loop to print numbers from 1
to 5
:
#include <iostream>
int main() {
int count = 1; // Initialization
do {
std::cout << "Count is: " << count << std::endl;
count++; // Update
} while (count <= 5); // Condition
return 0;
}
Output:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Explanation
- Initialization (
int count = 1
): A variable is declared and initialized to control the number of loop iterations. - Loop Body: The block of code inside the
do
is executed first. In this case, it prints the current value ofcount
and increments it. - Condition Check (
count <= 5
): After the loop body is executed, the condition is checked. If it istrue
, the loop runs again; if it isfalse
, the loop terminates.
Use Case: User Input Example
do-while
loops are often used for user input scenarios where you want to prompt the user until they provide valid input.
Example:
#include <iostream>
int main() {
int number;
do {
std::cout << "Enter a number between 1 and 10: ";
std::cin >> number;
} while (number < 1 || number > 10); // Condition for valid input
std::cout << "You entered: " << number << std::endl;
return 0;
}
- In this example, the program prompts the user to enter a number until a valid number (between 1 and 10) is provided.
- The input is requested at least once, ensuring that the user has the opportunity to provide input.
Infinite do-while
Loop
Similar to a while
loop, a do-while
loop can also create an infinite loop if the condition is always true
.
Example:
#include <iostream>
int main() {
int count = 0;
do {
std::cout << "This loop will run forever unless you break it!" << std::endl;
} while (true); // Always true
return 0;
}
To exit an infinite loop, you can use a control statement like break
.
Comparison with while
Loop
The primary difference between do-while
and while
loops is when the condition is evaluated:
while
Loop: The condition is checked before the loop body is executed. If the condition isfalse
, the loop may not execute at all.do-while
Loop: The loop body is executed first, and then the condition is checked. The loop will always execute at least once.
Example of while
vs do-while
:
#include <iostream>
int main() {
int count = 6;
// While Loop
while (count <= 5) {
std::cout << "This will not run." << std::endl; // This will not execute
count++;
}
// Do-While Loop
do {
std::cout << "This will run once." << std::endl; // This will execute once
count++;
} while (count <= 5);
return 0;
}
Output:
This will run once.
Summary
- The
do-while
loop is a control structure that guarantees at least one execution of the loop body, making it ideal for situations where input is required or when you need to perform an operation at least once. - The condition is checked after the loop body has executed, allowing for different behaviors compared to the
while
loop. - It is useful in user interaction scenarios, validating inputs, or running operations that need to happen once before checking for further conditions.
Using do-while
loops effectively allows you to create robust programs that handle input and conditions with ease, ensuring a user-friendly experience.