0Pricing
C# Academy · Lesson

break and continue

Control loop flow.

Controlling the Loop

Sometimes you don't want to process every item. C# gives you two keywords for this.

break stops the loop completely. continue skips to the next item.

What break Does

break exits the loop immediately. No more items are visited.

Use it when you found what you were looking for and there's no reason to keep going.

foreach (var n in nums)
{
    if (n == 0) break;
    Console.WriteLine(n);
}

All lessons in this course

  1. The foreach Loop
  2. Iterating Collections
  3. break and continue
  4. Common Iteration Mistakes
← Back to C# Academy