A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Loop Control: Break and Continue

College Depth 75 in the knowledge graph ☐ I know this ☆ Set as goal
528topics build on this
325prerequisites beneath it
See this on the map →
For LoopsWhile Loops→→Algorithm Design Basics
loops control break-continue

Core Idea

Break exits a loop immediately; continue skips the rest of the current iteration and proceeds to the next. These statements provide fine-grained control over loop execution without restructuring the loop condition.

How It's Best Learned

Write loops that use break (e.g., to exit early on a match) and continue (e.g., to skip invalid values). Compare with conditional guards.

Common Misconceptions

Explainer

Your for loops and while loops already let you repeat code, but sometimes you need to deviate from the normal iteration pattern. Maybe you are searching a list for a specific value and want to stop as soon as you find it — there is no point continuing through the remaining elements. Or maybe you are processing a list of inputs and want to skip invalid entries without stopping the entire loop. Break and continue give you these two escape hatches.

Break immediately exits the loop, jumping to the first statement after the loop body. Consider searching for a name in a list of 1,000 entries. Without break, a for loop would check all 1,000 entries even if the name is the very first one. With break, you add a condition inside the loop: `if (current == target) { found = true; break; }`. The moment the target is found, the loop ends. This is both more efficient and more expressive — the code clearly communicates that finding the item is the stopping condition.

Continue is more subtle. Instead of exiting the loop entirely, it skips the rest of the current iteration and jumps back to the loop's condition check (or update step, in a for loop). Imagine processing a list of numbers but wanting to ignore negative values. You could wrap the entire loop body in an `if (value >= 0)` block, but continue provides a flatter structure: `if (value < 0) { continue; }` at the top of the loop, followed by the normal processing code. The effect is the same — negative values are skipped — but the code avoids deeply nested if statements.

The most important rule to remember is that break and continue affect only the innermost enclosing loop. If you have a loop inside a loop (nested loops, which you will study soon), a break in the inner loop exits only the inner loop — the outer loop keeps running. This trips up many learners who expect break to exit all enclosing loops at once. Some languages offer labeled breaks to exit an outer loop, but the default behavior is always innermost-only. A second caution: while break and continue are useful tools, overusing them can make your code harder to follow. If a loop has three breaks and two continues scattered throughout, it becomes difficult to reason about when and how the loop terminates. Use them purposefully — for early exit on search or for skipping invalid data — rather than as a substitute for a well-structured loop condition.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → While Loops → For Loops → Loop Control: Break and Continue

Longest path: 76 steps · 325 total prerequisite topics

Prerequisites (2)

Leads To (1)