Jump Statements in C# – break, continue, return Explained with Examples
Jump statements in C# control the flow of execution by altering the natural order. They are used to:
break – Exit a loop or switch block prematurely.
continue – Skip the current iteration of a loop and jump to the next.
return – Exit from a method and optionally return a val
Example
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
Console.WriteLine("Breaking at i = " + i);
break;
}
Console.WriteLine("i = " + i);
}
Console.WriteLine("---");
for (int j = 1; j <= 5; j++)
{
if (j == 3)
{
Console.WriteLine("Skipping i = " + j);
continue;
}
Console.WriteLine("j = " + j);
}
}
}
Summary
In the first loop (for i = 1 to 5):
When i == 3, the break executes.
It exits the loop immediately, so values after 3 are not printed.
?? In the second loop:
When j == 3, the continue statement runs.
That iteration is skipped, and it jumps directly to j = 4.
?? In the third example:
If a == 0, the return statement runs early and exits the AddNumbers() method.
It doesn’t execute a + b, and instead returns 0.
Example
using System;
class Program
{
static void Main()
{
Console.WriteLine("Result: " + AddNumbers(5, 3));
Console.WriteLine("Result: " + AddNumbers(0, 3));
}
static int AddNumbers(int a, int b)
{
if (a == 0)
{
Console.WriteLine("Invalid input. Returning 0.");
return 0;
}
return a + b;
}
}