Coding/Learn/cpp/break continue
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
Course Certificate0%

Complete all lessons to earn

LearnLoop Control: break and continue
12 / 20 0%
Lesson 12 of 20
5 mins read

Loop Control: break and continue

PreviousNext Lesson
In Plain English

Sometimes you need fine-grained control over loop execution midway through a cycle. break immediately terminates the entire loop, jumping past its closing brace. continue skips the rest of the current iteration and jumps directly to the next cycle.

The Mental Model

"Imagine searching through a deck of cards for the Ace of Spades. As soon as you find it, you shout "Found it!" and stop searching (break). If you draw a red card you don't care about, you toss it aside and immediately flip the next card (continue)."

Deep Dive: How It Works

break: Instant loop termination. Essential for linear search algorithms once the target item is found.

continue: Skips only the current iteration. The loop does NOT stop; it advances straight to the increment step and checks the condition for the next cycle.

Nested Loops: break only exits the innermost loop containing it.

Syntax Blueprint

for (int i = 0; i < 10; i++) {
  if (i == 3) continue; // Skips 3
  if (i == 7) break;    // Stops loop at 7
  std::cout << i << " ";
}

continue skips the rest of the body; break stops the loop completely.

PointCore Rules to Remember

Point star
break Stops Everything: Instantly breaks out of the loop and resumes code right after the loop.
Point star
continue Skips to Next: Bypasses remaining code in this cycle without terminating the loop.
Point star
Search Optimization: Prevents wasteful iterations when an answer has already been found.

Common Beginner Traps & How to Fix Them

Mistake:Using continue in a while loop without incrementing the counter

Why it happens: If continue skips past the line with i++, i never changes, causing an infinite loop!

How to fix: Ensure the counter increments before calling continue in while loops.

Pro Tip: In clean code, use break early when finding targets to save CPU cycles and improve readability.

Live Interactive Example

Hit Run Code to see it live
Searching with break and Filtering with continue
C++ 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Output Console
Click "Run Code" to view the rendered output.
How it works: Odd numbers are bypassed with continue, and the loop terminates at 6 with break.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Stop a loop at 3

  • PointWrite a for loop from int i = 1 to 5.
  • PointIf i == 3, print "Found 3" and break the loop.
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates