How do I jump out of a foreach loop in C?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, the normal way to exit a foreach loop early is break. If you want to leave the entire method, use return instead. The key is to choose the control-flow statement that matches how far you want execution to jump.
Use break To Exit Only The Loop
break stops the current loop immediately and continues with the next statement after the loop.
Output:
- '
1' - '
2' - then execution continues after the loop
This is the standard answer when people ask how to "jump out" of a foreach.
Use return To Exit The Whole Method
If the loop is inside a method and you want to stop the method entirely, use return.
return is stronger than break because it leaves the current method, not just the loop.
Nested Loops Need Care
If the foreach is inside another loop, break exits only the innermost loop.
That break exits the foreach, but the outer for keeps running.
If you need to stop both loops, common options are:
- '
returnif leaving the method is acceptable' - a boolean flag checked by the outer loop
- refactoring the inner logic into a helper method
In C#, goto can also do it, but it is rarely the clearest choice.
yield break Is Different
If you are inside an iterator method that uses yield return, the matching loop-exit tool may be yield break.
This does not just break a loop. It ends the generated sequence entirely.
That distinction matters in iterator methods.
foreach Is Not The Same As LINQ Callbacks
People sometimes confuse a real foreach loop with LINQ or collection callbacks.
This is a real loop:
This is not:
Inside a lambda passed to List<T>.ForEach, break is not available because you are inside a delegate body, not a loop statement. Use a normal foreach when early exit matters.
Common Pitfalls
The biggest mistake is using return when you only meant to stop the loop. That exits the whole method and can skip important code that should run afterward.
Another mistake is expecting break in a nested loop to stop every surrounding loop. It only exits the innermost loop where it appears.
People also try to use break inside lambda callbacks such as List<T>.ForEach. That does not work because the callback is not the loop itself.
Finally, if you are inside an iterator method, remember that yield break has a different meaning from normal break.
Summary
- Use
breakto exit aforeachloop early. - Use
returnwhen you want to exit the entire method. - In nested loops,
breakexits only the innermost loop. - '
yield breakis the right tool inside iterator methods.' - Prefer a real
foreachover callback-based iteration when early exit is important.
Related reading
- How do I keep a label centered in WinForms?
- How do I keep my Async method thread safe?
- How do I list all loaded assemblies?
- How do I make a WinForms app go Full Screen
- How do I make calls to a REST API using C?
- How do I monitor clipboard content changes in C?
- How do I open an already opened file with a .net StreamReader?
- How do I programmatically get the GUID of an application in C with .NET?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.