C++ while Loop
In this page:
while (condition) {
// body
// update the loop variable
}
Basic while Loop
A while loop repeatedly runs its body for as long as its condition stays true, checking that condition again before every single iteration — including before the very first one, so if the condition starts false, the loop body never runs at all.
This makes while the right choice when you don't know in advance exactly how many times you'll need to repeat something.
Example: Basic while Loop
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare count and set it to 0
int count = 0;
// Repeat while count < 5 is true
while (count < 5) {
// Print to the console with cout
std::cout << count << std::endl;
count++;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Decrementing Loop Conditions
Subtracting from a loop counter each iteration, like count-- inside the loop, lets you count downward toward a target instead of upward, which is exactly how countdown timers and reverse iteration through data are typically implemented.
The loop's condition then usually checks for reaching zero or some lower bound.
Example: Decrementing Loop Conditions
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare count and set it to 5
int count = 5;
// Repeat while count > 0 is true
while (count > 0) {
// Print to the console with cout
std::cout << count << std::endl;
count--;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using Boolean Flags for Loop Control
Using a boolean variable as the loop's condition, such as while (keepGoing), lets code inside the loop body decide dynamically whether iteration should continue by changing that flag's value.
This is a common pattern for loops whose stopping point depends on some runtime event rather than a simple counter.
Example: Using Boolean Flags for Loop Control
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare keepGoing and set it to true
bool keepGoing = true;
// Declare count and set it to 0
int count = 0;
// Repeat while keepGoing is true
while (keepGoing) {
// Print to the console with cout
std::cout << count << std::endl;
count++;
// Check whether count >= 3
if (count >= 3) {
keepGoing = false;
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Numeric Limit Iterations
Multiplying or dividing a loop variable each pass, rather than adding a fixed amount, produces exponential rather than linear progress — useful for algorithms like binary search that repeatedly halve a search range, or for simulating compounding growth over time.
Example: Numeric Limit Iterations
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 1
int value = 1;
// Repeat while value < 100 is true
while (value < 100) {
// Print to the console with cout
std::cout << value << std::endl;
value *= 2;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using the break Statement
Calling break inside a while loop immediately exits the loop entirely, skipping any remaining iterations and jumping straight to the code right after it.
This is useful for stopping early the moment a specific condition is found, rather than continuing to check the loop's main condition on further passes that would be wasted work.
Example: Using the break Statement
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare count and set it to 0
int count = 0;
// Repeat while count < 10 is true
while (count < 10) {
// Check whether count == 3
if (count == 3) {
// Leave the switch/loop
break;
}
// Print to the console with cout
std::cout << count << std::endl;
count++;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting to update the loop variable, creating an infinite loop.
- Adding a semicolon after
while (x < 5);, making the loop body empty. - Getting an off-by-one error by using
<=instead of<.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: