C++ do-while loop
A do-while loop is a type of control flow statement in the C++ programming language.
It allows a block of code to be executed repeatedly until a specified condition is no longer true. The basic structure of a do-while loop is as follows:
javascript code
do
{
// code block to be executed
}
while (condition);
In this structure, the code block will always be executed at least once, regardless of whether or not the condition is initially true.
Once the code block has been executed, the condition is checked. If the condition is true, the code block is executed again.
This process is repeated until the condition becomes false.
One of the key differences between a do-while loop and a regular while loop is that in a do-while loop, the condition is checked after the code block has been executed.
In a regular while loop, the condition is checked before the code block is executed, which means that the code block may not be executed at all if the condition is false from the start.
The do-while loop is often used in situations where you want to ensure that a block of code is executed at least once, regardless of whether or not the condition is true.
For example, if you were writing a program to validate user input, you might use a do-while loop to repeatedly prompt the user for input until they enter a valid value.
Let's take a closer look at how a do-while loop works by exploring some examples.
Example 1: Printing the numbers 1 to 10
code
#include <iostream>
int main()
{
int i = 1;
do { std::cout << i << std::endl; i++;
}
while (i <= 10);
return 0;
}
In this example, we are using a do-while loop to print the numbers 1 to 10 to the console. The loop starts with the variable i initialized to 1.
The code block inside the loop simply prints the value of i to the console, and then increments i by 1.
The condition for the loop is that i must be less than or equal to 10.
This means that the loop will continue to execute as long as i is less than or equal to 10. Once i is greater than 10, the condition will be false, and the loop will terminate.
Because the code block is executed before the condition is checked, the loop will always execute at least once.
This means that the number 1 will be printed to the console before the condition is even checked.
Example 2: Validating user input
cCopy code
#include <iostream>
int main()
{
int age;
do { std::cout << "Enter your age: ";
std::cin >> age;
}
while (age < 0 || age > 120);
std::cout << "Your age is " << age << std::endl;
return 0;
}
In this example, we are using a do-while loop to repeatedly prompt the user to enter their age until they enter a valid value.
The loop starts by initializing the variable age to an undefined value.
The code block inside the loop prints a message to the console asking the user to enter their age, and then waits for input using the std::cin function.
The std::cin function reads input from the user and stores it in the variable age.
The condition for the loop is that age must be between 0 and 120, inclusive.
If the user enters a value outside of this range, the loop will execute again and prompt the user to enter their age again.
0 Comments