C++ do Keyword
The do keyword in C++ is used to create a do-while loop. This type of loop ensures that the body of the loop is executed at least once before checking the condition. The condition is evaluated at the end of the loop, making it different from other loop constructs like for and while, where the condition is evaluated before the loop body is executed.
Syntax
</>
Copy
do {
// Loop body
} while (condition);
- do
- The keyword that starts the
do-whileloop and ensures the loop body is executed at least once. - condition
- The boolean expression evaluated after the loop body. If true, the loop continues; otherwise, it terminates.
Examples
Example 1: Basic do-while Loop
This example demonstrates a simple do-while loop that prints numbers from 1 to 5.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);
return 0;
}
Output:
1 2 3 4 5
Explanation:
- The variable
iis initialized to 1. - The
doblock executes the loop body, printing the value ofi. - The value of
iis incremented after each iteration. - The loop continues until the condition
i <= 5becomes false.
Example 2: Input Validation with do-while Loop
This example demonstrates using a do-while loop to repeatedly prompt the user for input until a valid number is entered.
</>
Copy
#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;
}
Output:
Enter a number between 1 and 10: 15
Enter a number between 1 and 10: 5
You entered: 5
Explanation:
- The program uses a
do-whileloop to prompt the user for input. - The condition
number < 1 || number > 10ensures the loop continues until a valid number is entered. - Once the user enters a valid number, the loop terminates, and the program prints the number.
Example 3: Summing Numbers with do-while
This example demonstrates using a do-while loop to sum numbers until the user enters 0.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int number, sum = 0;
do {
cout << "Enter a number (0 to stop): ";
cin >> number;
sum += number;
} while (number != 0);
cout << "Total sum: " << sum << endl;
return 0;
}
Output:
Enter a number (0 to stop): 5
Enter a number (0 to stop): 10
Enter a number (0 to stop): 0
Total sum: 15
Explanation:
- The loop executes at least once, prompting the user to enter a number.
- The entered number is added to the variable
sum. - The loop continues until the user enters
0, at which point the total sum is printed.
Key Points about do Keyword
- The
dokeyword ensures the loop body executes at least once, regardless of the condition. - The condition is evaluated at the end of the loop, making
do-whileloops useful for scenarios like input validation and repeated tasks. - The loop terminates when the condition evaluates to
false. - It is essential to ensure the loop condition eventually becomes false to avoid infinite loops.
