Loops are essential for repeating similar operations and simplifying a programmer's life. Computers are happy to perform any number of repetitive actions; it's a piece of cake for them. In the C++ programming language, there are only four types of loops. In this topic, you will explore two of them: the while loop and the do-while loop.
The while loop
The while loop is the simplest of the four loops and is very similar to an if/else branch.
The while loop consists of a block of code and a condition (a Boolean expression). If the condition is true, the code within the block is executed. This code repeats until the condition becomes false. Since this loop checks the condition before the block is executed, the control structure is also known as a pre-test loop. You can think of the while loop as a repetitive if/else operator.
The basic syntax of the while loop is the following:
while (condition) {
// some code here
// this code will repeat as long as the condition is true
}
A loop's body can contain any correct C++ statements, including conditional statements and even other loops, the latter being called nested loops.
It is also possible to write an infinite loop if the condition is invariably true:
while (true) {
// code will repeat indefinitely
}
But be very careful with such experiments, you need to clearly understand what you are doing and how to stop it!
#include <iostream>
int main() {
int count = 0;
while (count < 5) {
std::cout << "Count: " << count << std::endl;
count++;
}
return 0;
}
In this example, the while loop will continue executing the code block as long as the condition count < 5 is true. It will print the current value of count and increment it by 1 with each iteration. The loop will stop once the condition becomes false. The output will be:
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Here is another example. The following program displays English letters in a single line:
#include <iostream>
int main() {
char letter = 'A';
while (letter <= 'Z') {
std::cout << letter;
letter++;
}
return 0;
}
The program takes the first letter 'A' and then goes on like this:
- if the
letteris less than or equal to'Z'the program goes to the loop's body; - inside the body, it prints the current character and
lettertakes the next alphabet letter.
The program prints:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Remember that it is possible to get the next character according to the Unicode table by using the increment operator. After the code execution, the letter will equal [.
The do-while loop
In the do-while loop, the body is executed first, while the condition is tested afterwards. If the condition is true, statements within the block are executed again. This repeats until the condition becomes false. Because do-while loops check the condition after the block is executed, the control structure is often also known as a post-test loop.
In contrast to the while loop, which tests the condition before the code within the block is executed, the do-while loop is an exit-condition loop. So, the code within the block is always executed at least once.
This loop contains three parts: the do keyword, a body, and while(condition):
do {
// body: do something
} while (condition); // check condition and if true, go to body
A good example of using it is a program that reads data from the standard input until a user enters a certain number or string. The following program reads integer numbers from the standard input and displays them. And, the program runs indefinitely until you press the number 0.
The following example demonstrates the do-while loop:
#include <iostream>
int main() {
int value;
std::cout << "Input value: " << std::endl;
do {
std::cin >> value;
std::cout << "You entered" << value << std::endl;
} while (value != 0);
return 0;
}
If the number 0 is entered, the program prints it and then stops. Note that, like the while loop, the do-while loop can be infinite.
In practice, the do-while loop is used less than the while loop. It is used when code inside the loop must be executed at least once.
Conclusion
There are different ways to perform some fragments of your code several times. In this topic, we've discussed two alternative ways to use loops that are based on conditional statement evaluation. If you want to check the condition first and, based on the result, perform the operations or ignore them at all, the while loop is your choice. If you want to do one iteration of the loop in any case and then evaluate the condition for repetition, then choose do-while.