Foreach, break and continue
A foreach loop visits the elements of a collection without requiring a manually managed index. Use continue to skip the rest of the current iteration and break to stop the nearest enclosing loop or switch statement. In a switch nested inside a loop, break exits the switch, not the loop. These tools are useful for scanning available inventory.
int[] stock = { 0, 3, 1 };int total = 0;foreach (int count in stock){ total += count;}System.Console.WriteLine(total);Each array element contributes to the accumulator exactly once.
Expected output (reasoned, not executed):
4Preview a collection
An array stores an ordered, fixed-length sequence of values of one element type. int[] stock = { 0, 3, 1 }; creates a small array of integers; arrays are covered in detail in chapter 7. foreach (int count in stock) introduces one ordinary iteration variable for each element. For an array, traversal follows index order.
Skip or stop
continue skips the remainder of the current loop body and proceeds to the next iteration if one exists. break terminates the nearest enclosing loop (or switch). A search can break as soon as a suitable item is found; a counting task generally needs to inspect every item. An empty array yields zero iterations.
Keep search state separate
For ordinary foreach over these arrays, the iteration variable cannot be reassigned. Use a separate result variable to remember a match; later array lessons teach direct element updates by index. Initialize a found flag to false, or use a nullable result to represent absence. In nested loops, an unlabeled break affects only the innermost applicable loop or switch in C# 14.
Examples and walkthroughs
int[] stock = { 0, 3, 0, 2 };foreach (int count in stock){ if (count == 0) { continue; } System.Console.WriteLine(count);}The zero values skip printing; the loop still visits the remaining elements.
Expected output (reasoned, not executed):
32int[] stock = { 0, 0, 4, 9 };int? first = null;foreach (int count in stock){ if (count > 0) { first = count; break; }}System.Console.WriteLine(first?.ToString() ?? "None");The first positive count is saved and the loop ends before visiting nine. Null represents no match.
Expected output (reasoned, not executed):
4Common mistake and correction
Using break when you mean continue stops scanning entirely. In an array beginning with zero, breaking at zero would miss every later positive count. Also avoid trying count++ on the ordinary foreach iteration variable; update a separate accumulator instead.
Try it yourself
Find the first positive count in { 0, 0, 5, 2 }, { 0, 0 }, and an empty int array. Then count all positive elements in the first array without stopping early.
Next: String operations and interpolation. These examples target C# 14 / net10.0 and are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Foreach, break and continue — knowledge check
Apply the lesson concepts to the following scenarios.
What is the array traversal order for ordinary foreach?
What does continue skip?
What does break do in a simple foreach?
What should a search return when no positive count exists and zero is not a match?
How many body executions occur for an empty array?