Coding/Learn/csharp/break and continue
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
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

Fine-grained loop control gives you power over execution flow. break terminates the loop immediately and jumps past its closing brace. continue skips the rest of the current cycle and jumps directly to the next iteration.

The Mental Model

"Searching through a box for your lost keys: As soon as your hand grabs the keys, you shout "Found them!" and stop searching (break). If you pick up a piece of junk mail, you drop it and instantly grab the next item (continue)."

Deep Dive: How It Works

break: Halts the loop immediately. Crucial for search algorithms once the target is found to avoid wasting CPU cycles.

continue: Skips the remaining code inside the loop body for this cycle only; loop advances to the next iteration.

Nested Loops: break only breaks out of the innermost loop containing it.

Syntax Blueprint

for (int i = 0; i < 10; i++) {
  if (i % 2 != 0) continue; // Skip odds
  if (i == 6) break;        // Stop at 6
  Console.WriteLine(i);
}

continue skips to next cycle; break terminates the entire loop.

PointCore Rules to Remember

Point star
break Stops Everything: Instantly exits the loop body.
Point star
continue Skips Cycle: Bypasses remaining code in this specific cycle.
Point star
Search Efficiency: Saves valuable CPU cycles when searching collections.

Common Beginner Traps & How to Fix Them

Mistake:Using continue in a while loop after the counter increment

Why it happens: If continue skips before i++, i never increments, causing an infinite loop.

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

Pro Tip: Use break early in search routines to keep your code fast and responsive!

Live Interactive Example

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

Your Turn: Micro Challenge

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

Micro Exercise

Stop 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
8
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