C# Break and Continue

Control loop execution flow in C#

🔄 What are Break and Continue?

Break and continue are control statements that change how loops execute. Break exits the loop immediately, while continue skips the current iteration and moves to the next one.


// Simple break example
for (int i = 0; i < 5; i++)
{
    if (i == 3) break;
    Console.WriteLine(i);
}
// Output: 0, 1, 2
                                    

Understanding Break and Continue

🛑

Break Statement

Exits the loop completely

if (condition)
    break;
⏭️

Continue Statement

Skips to next iteration

if (condition)
    continue;
🔁

Loop Control

Works with for, while, foreach

while (true)
{
    if (done) break;
}
🎯

Nested Loops

Only affects innermost loop

for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
        if (j == 1) break;

🔹 The Break Statement

The break statement exits a loop immediately when a condition is met, skipping remaining iterations. It is commonly used in search algorithms to stop once an item is found, improving efficiency. For example, inside a for loop, break halts execution and proceeds to the code after the loop.

// Break in a for loop
for (int i = 1; i <= 10; i++)
{
    if (i == 5)
    {
        break; // Exit loop when i equals 5
    }
    Console.WriteLine(i);
}
Console.WriteLine("Loop ended!");

// Output:
// 1
// 2
// 3
// 4
// Loop ended!

Output:

1
2
3
4
Loop ended!

🔹 Break in While Loop

Break is especially useful in while loops to exit based on conditions:

int count = 0;
while (true) // Infinite loop
{
    Console.WriteLine("Count: " + count);
    count++;
    
    if (count >= 3)
    {
        break; // Exit the infinite loop
    }
}
Console.WriteLine("Finished!");

// Output:
// Count: 0
// Count: 1
// Count: 2
// Finished!

Output:

Count: 0
Count: 1
Count: 2
Finished!

🔹 The Continue Statement

The continue statement skips the current iteration of a loop and proceeds to the next one. It is useful for filtering values, like skipping even numbers in a sequence. For example, if (i % 2 == 0) continue; ensures only odd numbers are processed, without exiting the loop entirely.

// Continue in a for loop
for (int i = 1; i <= 5; i++)
{
    if (i == 3)
    {
        continue; // Skip when i equals 3
    }
    Console.WriteLine(i);
}

// Output:
// 1
// 2
// 4
// 5

Output:

1
2
4
5

🔹 Continue with Even/Odd Numbers

A practical example of continue - printing only odd numbers:

// Print only odd numbers
for (int i = 1; i <= 10; i++)
{
    if (i % 2 == 0) // If number is even
    {
        continue; // Skip even numbers
    }
    Console.WriteLine(i + " is odd");
}

// Output:
// 1 is odd
// 3 is odd
// 5 is odd
// 7 is odd
// 9 is odd

Output:

1 is odd
3 is odd
5 is odd
7 is odd
9 is odd

🔹 Break vs Continue Comparison

Understanding the difference between break and continue:

Key Differences:

  • Break: Exits the loop completely and continues with code after the loop
  • Continue: Skips current iteration but continues with the next iteration
  • Break: Loop stops executing entirely
  • Continue: Loop keeps running, just skips some code
// Comparing break and continue
Console.WriteLine("Using BREAK:");
for (int i = 1; i <= 5; i++)
{
    if (i == 3) break;
    Console.WriteLine(i);
}

Console.WriteLine("\nUsing CONTINUE:");
for (int i = 1; i <= 5; i++)
{
    if (i == 3) continue;
    Console.WriteLine(i);
}

// Output:
// Using BREAK:
// 1
// 2
//
// Using CONTINUE:
// 1
// 2
// 4
// 5

Output:

Using BREAK:
1
2

Using CONTINUE:
1
2
4
5

🔹 Nested Loops with Break

Break only exits the innermost loop it's placed in:

// Break in nested loops
for (int i = 1; i <= 3; i++)
{
    Console.WriteLine("Outer loop: " + i);
    
    for (int j = 1; j <= 3; j++)
    {
        if (j == 2)
        {
            break; // Only exits inner loop
        }
        Console.WriteLine("  Inner loop: " + j);
    }
}

// Output:
// Outer loop: 1
//   Inner loop: 1
// Outer loop: 2
//   Inner loop: 1
// Outer loop: 3
//   Inner loop: 1

Output:

Outer loop: 1
  Inner loop: 1
Outer loop: 2
  Inner loop: 1
Outer loop: 3
  Inner loop: 1

🔹 Real-World Example: Search

Using break to exit when an item is found:

// Search for a number in an array
int[] numbers = { 10, 20, 30, 40, 50 };
int searchFor = 30;
bool found = false;

for (int i = 0; i < numbers.Length; i++)
{
    if (numbers[i] == searchFor)
    {
        Console.WriteLine("Found " + searchFor + " at index " + i);
        found = true;
        break; // Stop searching once found
    }
}

if (!found)
{
    Console.WriteLine(searchFor + " not found");
}

// Output:
// Found 30 at index 2

Output:

Found 30 at index 2

🧠 Test Your Knowledge

What does the break statement do in a loop?