← Back to C++ Course | Chapter 4: Control Flow | Lesson 9 of 13

C++ while Loop

A while loop repeats steps as long as a condition stays true, like stirring soup until it is hot. It checks before each round.
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to update the loop variable, creating an infinite loop.
  2. Adding a semicolon after while (x < 5);, making the loop body empty.
  3. Getting an off-by-one error by using <= instead of <.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.