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

C++ break & continue

break leaves a loop right away, like walking out of a game once you win, and continue skips only the current round. They give you control inside repeating steps.
Syntax
cpp
break;     // leave the loop immediately
continue;  // skip to the next iteration

The break Statement

break immediately terminates the loop it's inside, jumping straight to the first line of code after the loop's closing brace, regardless of whether the loop's normal condition would have kept it running.

This is typically used to stop searching the moment a target value is found, avoiding wasted iterations.

Example: The break Statement

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

// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 10; i++) {
		// Check whether i == 5
		if (i == 5) {
			// Leave the switch/loop
			break;
		}
		// Print to the console with cout
		std::cout << i << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

The continue Statement

continue skips only the remaining code in the current iteration and jumps straight to the loop's next pass, rather than exiting the loop entirely like break does.

It's useful for filtering — for example, skipping over negative numbers in a loop that only needs to process positive ones.

Example: The continue Statement

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

// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 5; i++) {
		// Check whether i == 2
		if (i == 2) {
			// Skip the rest of this iteration
			continue;
		}
		// Print to the console with cout
		std::cout << i << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

break in Nested Loops

Inside nested loops, break only exits the innermost loop it's directly written in — the outer loop is unaffected and continues iterating normally.

If you need to escape multiple nested loops at once, a common approach is a boolean flag checked in the outer loop, since C++ has no built-in labeled break.

Example: break in Nested Loops

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

// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Loop: repeat while the condition holds
		for (int j = 0; j < 3; j++) {
			// Check whether j == 1
			if (j == 1) {
				// Leave the switch/loop
				break;
			}
			// Print to the console with cout
			std::cout << i << "," << j << " ";
		}
	}
	// Print to the console with cout
	std::cout << std::endl;
	return 0;
}

continue in Nested Loops

Similarly, continue inside a nested loop only skips ahead within the loop it's directly inside, leaving the outer loop's iteration count untouched.

This distinction between affecting the inner loop versus the outer loop is a common source of confusion when loops are deeply nested.

Example: continue in Nested Loops

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

// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 2; i++) {
		// Loop: repeat while the condition holds
		for (int j = 0; j < 3; j++) {
			// Check whether j == 1
			if (j == 1) {
				// Skip the rest of this iteration
				continue;
			}
			// Print to the console with cout
			std::cout << i << "," << j << " ";
		}
	}
	// Print to the console with cout
	std::cout << std::endl;
	return 0;
}

Control with User Input

Combining break and continue with conditions based on user input lets a program respond dynamically at runtime — for example, breaking out of an input loop as soon as the user types quit, or using continue to silently skip invalid entries and keep prompting.

Example: Control with User Input

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

// Program execution starts in main()
int main() {
	int hardcodedInputs[] = {3, 5, -1, 7};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 4; i++) {
		// Declare value and set it to hardcodedInputs[i]
		int value = hardcodedInputs[i];
		// Check whether value < 0
		if (value < 0) {
			// Leave the switch/loop
			break;
		}
		// Print to the console with cout
		std::cout << value << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	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. Using break and expecting it to exit both loops in a nested loop, when it only exits the innermost.
  2. Placing continue before the increment in a while loop, causing an infinite loop.
  3. Using break outside a loop or switch, which is a compile error.
Chapter Summary
  • Booleans and boolean expressions drive decisions with if, if-else, and else-if statements.
  • Nested and logical conditions and the switch statement handle more complex choices.
  • while, do-while, and for loops repeat code, with break and continue controlling the 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.