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

C++ do-while Loop

A do-while loop does the steps first and checks afterward, like tasting food before deciding to add salt. It always runs at least once.
Syntax
cpp
do {
  // body runs at least once
} while (condition);

Basic do-while Loop

A do-while loop is a post-test loop: it runs the body first and only checks the condition afterward, at the bottom of the loop, which is the opposite order from a regular while loop's condition-first check.

This structural difference is what guarantees the loop body always executes at least once.

Example: Basic do-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;
	// do-while: run the body once, then repeat while the condition is true
	do {
		// Print to the console with cout
		std::cout << count << std::endl;
		count++;
	} while (count < 5);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Guaranteeing At Least One Run

Unlike a while loop, which might never run its body if the condition starts false, a do-while loop always executes at least one full pass before the condition is even evaluated.

This makes it the right structural choice whenever 'do this at least once, then repeat if needed' is genuinely what the logic calls for.

Example: Guaranteeing At Least One Run

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare count and set it to 10
	int count = 10;
	// do-while: run the body once, then repeat while the condition is true
	do {
		// Print to the console with cout
		std::cout << "Runs once even though condition is false" << std::endl;
		count++;
	} while (count < 5);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Simulating User Menu Choices

Interactive menus are a textbook use case for do-while, since you naturally want to display the menu and get the user's first choice before there's anything to check — the loop then repeats showing the menu until the user picks an exit option.

A regular while loop would need an awkward workaround to show the menu before its condition could even be evaluated.

Example: Simulating User Menu Choices

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int choice;
	// Declare attempts and set it to 0
	int attempts = 0;
	int hardcodedInputs[] = {5, 2};
	// do-while: run the body once, then repeat while the condition is true
	do {
		choice = hardcodedInputs[attempts];
		// Print to the console with cout
		std::cout << "Menu choice: " << choice << std::endl;
		attempts++;
	} while (choice != 2);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Data Validation Simulation

Input validation follows the same pattern: you have to actually prompt for and receive input before you can check whether it's valid, which is exactly the 'act first, check after' order a do-while loop provides.

This avoids needing to fake an initial value just to satisfy a while loop's upfront condition check.

Example: Data Validation Simulation

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int age;
	int hardcodedInputs[] = {-5, 25};
	// Declare attempts and set it to 0
	int attempts = 0;
	// do-while: run the body once, then repeat while the condition is true
	do {
		age = hardcodedInputs[attempts];
		// Print to the console with cout
		std::cout << "Entered age: " << age << std::endl;
		attempts++;
	} while (age < 0);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested do-while Loops

Nesting do-while loops inside each other works the same way as nesting any other loop type, and is useful for building grid-based patterns or processing multi-dimensional data where each dimension needs to run at least once regardless of any condition, such as generating a fixed-size table.

Example: Nested do-while Loops

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare i and set it to 1
	int i = 1;
	// do-while: run the body once, then repeat while the condition is true
	do {
		// Declare j and set it to 1
		int j = 1;
		// do-while: run the body once, then repeat while the condition is true
		do {
			// Print to the console with cout
			std::cout << i << "," << j << " ";
			j++;
		} while (j <= 2);
		// Print to the console with cout
		std::cout << std::endl;
		i++;
	} while (i <= 2);
	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 the semicolon after while (condition) at the end of a do-while, which causes a compile error.
  2. Forgetting that the body runs at least once even if the condition is false.
  3. Never changing the variable in the condition, creating an infinite loop.

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.