C#
foreach loop
programming
break statement
loop control

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.

Browse interview questions

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.

csharp
1using System;
2
3var numbers = new[] { 1, 2, 3, 4, 5 };
4
5foreach (var number in numbers)
6{
7    if (number == 3)
8    {
9        break;
10    }
11
12    Console.WriteLine(number);
13}
14
15Console.WriteLine("Loop finished");

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.

csharp
1using System;
2
3static void PrintUntilThree(int[] numbers)
4{
5    foreach (var number in numbers)
6    {
7        if (number == 3)
8        {
9            return;
10        }
11
12        Console.WriteLine(number);
13    }
14
15    Console.WriteLine("This line runs only if 3 was not found.");
16}
17
18PrintUntilThree(new[] { 1, 2, 3, 4 });

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.

csharp
1for (int row = 0; row < 3; row++)
2{
3    foreach (var value in new[] { 10, 20, 30 })
4    {
5        if (value == 20)
6        {
7            break;
8        }
9
10        Console.WriteLine($"row={row}, value={value}");
11    }
12}

That break exits the foreach, but the outer for keeps running.

If you need to stop both loops, common options are:

  • 'return if 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.

csharp
1using System.Collections.Generic;
2
3static IEnumerable<int> ReadUntilThree(IEnumerable<int> numbers)
4{
5    foreach (var number in numbers)
6    {
7        if (number == 3)
8        {
9            yield break;
10        }
11
12        yield return number;
13    }
14}

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:

csharp
1foreach (var value in values)
2{
3    if (value == target)
4        break;
5}

This is not:

csharp
1values.ToList().ForEach(value =>
2{
3    // break is not valid here
4});

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 break to exit a foreach loop early.
  • Use return when you want to exit the entire method.
  • In nested loops, break exits only the innermost loop.
  • 'yield break is the right tool inside iterator methods.'
  • Prefer a real foreach over callback-based iteration when early exit is important.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.