Jump Statement in C#: Break, Continue, Goto, Return and Throw

A complete guide to jump statements in C#, covering break, continue, goto, return, and throw with practical examples of each.

What are Jump Statements?

Jump statements alter the normal sequential flow of a program, allowing execution to skip, exit, or transfer control to a different point in the code based on specific conditions.

break and continue

The break statement immediately exits the nearest enclosing loop or switch block, while continue skips the rest of the current iteration and moves directly to the next one, both giving you finer control over loop execution.

The goto Statement

The goto statement transfers control directly to a labeled statement elsewhere in the code. While powerful, it's used sparingly in modern C# since it can make code harder to follow if overused.

return and throw

The return statement exits a method and optionally sends a value back to the caller, while the throw statement raises an exception, immediately halting normal execution to signal that something has gone wrong.

Conclusion: Jump statements give you precise control over program flow beyond simple sequential execution. Used thoughtfully, they make your loops, methods, and error handling far more expressive and efficient.

All Five Jump Statements in One Program

Seeing every statement side by side makes the differences clear. The example below scans a list of order amounts, skips invalid ones, stops at a sentinel value, and raises an error for bad input.

using System;

class Orders
{
    static int SumUntilStop(int[] amounts)
    {
        if (amounts == null)
            throw new ArgumentNullException(nameof(amounts));   // throw

        int total = 0;
        foreach (int amount in amounts)
        {
            if (amount < 0) continue;    // skip bad entries
            if (amount == 0) break;      // 0 means "stop reading"
            total += amount;
        }
        return total;                     // send result back
    }

    static void Main()
    {
        Console.WriteLine(SumUntilStop(new[] { 100, -5, 250, 0, 900 }));  // 350
    }
}

Walk through it: 100 is added, -5 hits continue and is skipped, 250 is added, 0 triggers break, so 900 is never read. The method then returns 350.

goto in a switch and Nested Loops

C# does not allow one case to fall silently into the next. If you really want to share code between cases, goto case makes that jump explicit.

switch (plan)
{
    case "premium":
        Console.WriteLine("Adding premium extras");
        goto case "basic";
    case "basic":
        Console.WriteLine("Adding basic features");
        break;
    default:
        Console.WriteLine("Unknown plan");
        break;
}

Another place you may see goto is escaping from deeply nested loops, because break leaves only the innermost loop. A cleaner alternative is to move the loops into a separate method and use return as soon as the item is found. That is easier to read and to test.

Which Statement Should You Reach For?

SituationUse
Ignore one item and keep loopingcontinue
Stop the loop once a condition is metbreak
Finish a method early, with or without a valuereturn
Input or state is invalid and the caller must knowthrow
Share code between switch casesgoto case

Modern C# also lets you use throw as an expression, for example _name = name ?? throw new ArgumentNullException(nameof(name));, which keeps guard checks short.

Pitfalls to Watch For

  • Placing continue before the counter update in a while loop, which creates an endless loop.
  • Expecting break inside a nested loop to exit the outer loop too.
  • Using exceptions for normal control flow; throwing is slower and hides intent.
  • Writing throw ex; inside a catch block, which resets the stack trace. Use plain throw; to rethrow.
  • Expecting return inside a try to skip cleanup; the finally block still runs before the method exits.

A Note on yield break

Inside an iterator method, one that uses yield return to produce values lazily, the plain return keyword is not allowed for ending the sequence. Use yield break instead. It tells the caller's foreach loop that no more items are coming, which is handy when a stop condition is reached halfway through generating results. Think of it as the iterator version of return, and keep it in mind when reading LINQ-style helper code.

Ready to Master C# Programming Course?

Join Uncodemy's hands-on training program with expert mentors and real-world projects.