<style> .reveal-up, .reveal-left, .reveal-right, .reveal-stagger > * { opacity: 1 !important; transform: none !important; } </style>
  1. Learn
  2. C#
  3. Control flow
All lessons in this course

Lesson 10 of 60 12 minBeginner

break, continue and nested loops

Control a loop from the inside: stop it early with break, skip one round with continue, and put loops inside loops.

Sometimes you don’t want every iteration. You might want to stop as soon as you find something, or skip items you don’t care about. Two keywords do this: break and continue.

KeywordWhat it does
breakLeaves the loop immediately. Execution continues after the loop’s closing brace.
continueSkips the rest of this iteration and jumps to the next one (in a for, the update i++ still runs).
Interactive

break vs continue

Choose a keyword and the number that triggers it. Each box is one iteration of the loop.

Trigger when:
i = 1printed
i = 2printed
i = 3skipped
i = 4printed
i = 5printed
i = 6skipped
i = 7printed
i = 8printed
i = 9skipped
i = 10printed

Printed: 1 2 4 5 7 8 10

LoopControl.cs
for (int i = 1; i <= 10; i++){    if (i % 3 == 0)        continue;   // skip the rest, go to i++    Console.Write(i + " ");}

break: stop when you have the answer

FindFirst.cs
int[] loads = { 12, 18, 25, 40, 33 };int limit = 30;int position = -1; for (int i = 0; i < loads.Length; i++){    if (loads[i] > limit)    {        position = i;        break;          // found it: no need to check the rest    }}Console.WriteLine(position >= 0 ? $"First overload at index {position}" : "All OK");
Output
First overload at index 3

Without break the loop would carry on to the end and position would end up as the last match, not the first.

continue: skip what you don’t need

SkipInvalid.cs
string[] inputs = { "10", "abc", "25", "", "5" };int sum = 0; foreach (string s in inputs){    if (!int.TryParse(s, out int n))        continue;       // ignore anything that isn't a number     sum += n;}Console.WriteLine($"Sum of valid numbers: {sum}");
Output
Sum of valid numbers: 40

Loops inside loops

A nested loop is a loop inside the body of another loop. For every single iteration of the outer loop, the inner loop runs completely.

Table.cs
for (int row = 1; row <= 3; row++){    for (int col = 1; col <= 4; col++)    {        Console.Write($"{row * col,4}");   // ,4 = right-align in 4 spaces    }    Console.WriteLine();                     // new line after each row}
Output
   1   2   3   4
   2   4   6   8
   3   6   9  12

The outer loop runs 3 times and the inner loop 4 times for each, so the inner body runs 3 × 4 = 12 times. Nested loops multiply, so keep an eye on performance with big data.

Patterns with nested loops

Triangle.cs
int height = 4;for (int i = 1; i <= height; i++){    for (int j = 1; j <= i; j++)        Console.Write("* ");    Console.WriteLine();}
Output
* 
* * 
* * * 
* * * * 

break only leaves ONE loop

In nested loops, break exits only the innermost loop it is in. To stop both, use a flag variable, or put the loops in a method and return.

FindPair.cs
int[] nums = { 3, 8, 1, 6 };int target = 9;bool found = false; for (int i = 0; i < nums.Length && !found; i++){    for (int j = i + 1; j < nums.Length; j++)    {        if (nums[i] + nums[j] == target)        {            Console.WriteLine($"{nums[i]} + {nums[j]} = {target}");            found = true;            break;      // leaves the inner loop; the flag stops the outer one        }    }}
Output
3 + 6 = 9
Quick check

What does this print? for (int i = 1; i <= 5; i++) { if (i == 3) continue; Console.Write(i); }

Quick check

An outer loop runs 5 times and its inner loop runs 10 times per outer iteration. How many times does the inner body run?

Practice

Print all numbers from 1 to 30, but skip numbers divisible by 3, and stop completely when you reach a number greater than 20 that is divisible by 7.

Show a hint

Check the stop condition first (i > 20 && i % 7 == 0 → break), then the skip condition (i % 3 == 0 → continue).

Show the solution
Solution.cs
for (int i = 1; i <= 30; i++){    if (i > 20 && i % 7 == 0) break;    if (i % 3 == 0) continue;    Console.Write(i + " ");}
Output
1 2 4 5 7 8 10 11 13 14 16 17 19 20

Remember

  • break leaves the loop now; continue skips to the next iteration.
  • In nested loops the inner loop runs fully for every outer iteration.
  • break exits only the innermost loop; use a flag or return to leave all.
  • In while loops, update the counter before any continue.
0 of 60 lessons completed