Jump Statements
Jump statements transfer control immediately from one place to another. They interrupt the normal top-to-bottom flow of statements.
That sounds dramatic, but jump statements are common and useful when they express intent clearly. They let you:
- stop a loop early
- skip the rest of the current iteration
- leave a method
- signal an error
The most common jump statements in everyday C# are:
breakcontinuereturnthrow
There is also goto, but it is rare in modern C# and should usually be avoided in beginner code.
How jump flow changes execution
flowchart TD
A["Inside loop or method"] --> B{"Normal statement or jump?"}
B -- Normal --> C["Continue to next statement"]
B -- continue --> D["Skip rest of current loop iteration"]
B -- break --> E["Exit nearest loop or switch"]
B -- return --> F["Exit current method"]
B -- throw --> G["Transfer control to exception handling"]The key idea is that jump statements do not just do work. They change where execution goes next.
break
break exits the nearest enclosing loop or switch.
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
Console.WriteLine(i);
}This prints 0, 1, 2, and 3, then stops the loop completely.
Use break when the loop's job is finished before reaching its normal end.
continue
continue skips the rest of the current loop iteration and starts the next one.
for (int i = 0; i < 5; i++)
{
if (i == 2)
{
continue;
}
Console.WriteLine(i);
}This prints 0, 1, 3, and 4. When i is 2, the remaining statements in the loop body are skipped.
return
return exits the current method. If the method has a return type, it also provides the result.
static int Square(int number)
{
return number * number;
}You can also use return; in a void method to leave early.
static void PrintName(string? name)
{
if (string.IsNullOrWhiteSpace(name))
{
return;
}
Console.WriteLine(name);
}This pattern is often called an early return. It can reduce nesting and make code easier to follow.
throw
throw stops normal execution and signals an exception.
static int Divide(int left, int right)
{
if (right == 0)
{
throw new DivideByZeroException("Right operand cannot be zero.");
}
return left / right;
}Unlike break or return, throw does not merely move to another nearby statement. It transfers control into exception-handling flow.
goto
goto jumps to a labeled statement. It exists in C#, but it is rarely the best choice.
// Example only; usually avoid this style.
start:
Console.WriteLine("Hello");Beginners should usually avoid goto because it can make flow harder to trace.
A worked example
Suppose you want to search a list of product codes and stop as soon as a match is found.
string[] productCodes = { "A100", "B205", "C310", "D450" };
string searchCode = "C310";
bool found = false;
foreach (string code in productCodes)
{
if (code != searchCode)
{
continue;
}
Console.WriteLine($"Found product: {code}");
found = true;
break;
}
if (!found)
{
Console.WriteLine("Product not found.");
}Trace the flow carefully:
- The loop starts with
A100. - Because it does not match,
continueskips to the next iteration. - The same happens for
B205. - When
C310is reached, thecontinuepath is not taken. - The program prints the match, sets
foundtotrue, and usesbreakto exit the loop. - The final
ifsees thatfoundis true, so it does not print the “not found” message.
This example shows how continue and break solve different problems:
continueskips irrelevant workbreakstops the loop once the goal is reached
Common mistakes
- Using too many jump statements in one block, which can make flow hard to read.
- Forgetting that
breakexits only the nearest loop orswitch. - Using
continuewhen a simpleifblock would be clearer. - Throwing exceptions for normal expected cases that should be handled with regular conditions instead.
Summary
Jump statements change where execution goes next.
The most important ones are:
breakto leave a loop orswitchcontinueto skip to the next loop iterationreturnto leave a methodthrowto signal an exception
Used well, they simplify code. Used carelessly, they can make code feel scattered. The goal is always clearer control flow, not more surprising control flow.
Practice
Write a loop that prints numbers from 1 to 10 but skips 5 using continue.
As a second exercise, write a method that returns early when a string argument is null or empty, and otherwise prints the string in uppercase.