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
- The foreach Loop
- Iterating Collections
- break and continue
- Common Iteration Mistakes