C# > Core C# > Control Flow > break and continue Statements

Using 'continue' to Skip to the Next Iteration

This code snippet illustrates how the continue statement skips the rest of the current iteration of a loop and proceeds to the next iteration. It's useful for skipping over specific cases without terminating the entire loop.

Code Example: Skipping Even Numbers in a 'for' Loop

This example uses a for loop that iterates from 1 to 10. Inside the loop, there's an if statement that checks if the current number i is even (divisible by 2). If it is, the continue statement is executed. This skips the rest of the current iteration, and the loop proceeds to the next value of i. As a result, only odd numbers are printed to the console.

using System;

public class ContinueExample
{
    public static void Main(string[] args)
    {
        for (int i = 1; i <= 10; i++)
        {
            if (i % 2 == 0)
            {
                continue; // Skip even numbers
            }
            Console.WriteLine("Odd Number: " + i);
        }
        Console.WriteLine("Loop finished.");
    }
}

Concepts Behind the Snippet

The continue statement alters the normal flow of a loop by skipping the remaining code in the current iteration. This allows you to efficiently handle specific cases within a loop without adding nested if statements or complex logic. It maintains the loop's overall structure while bypassing certain operations.

Real-Life Use Case

Imagine processing a batch of data where some entries are invalid or irrelevant. The continue statement allows you to skip these invalid entries and proceed with processing the valid ones. For example, you might skip over empty lines when reading a file, or ignore negative values when calculating an average.

Best Practices

Use continue to simplify code by avoiding deeply nested if statements. Ensure the logic for skipping iterations is clear and well-documented. Avoid overuse, as it can sometimes make the loop's behavior less transparent. Consider whether refactoring the loop's condition might be a better alternative in some cases.

When to Use 'continue'

Use continue when you need to skip a portion of the loop's body based on a specific condition, but you still want the loop to continue executing for the remaining iterations. It's particularly useful when dealing with exceptional or irrelevant cases within the loop.

Alternatives

You can often achieve the same result as continue by using an if statement to conditionally execute the remaining code in the loop. However, continue can sometimes be more readable, especially when the skipping condition is simple and the code to be skipped is extensive.

Pros of Using 'continue'

  • Improved Readability: Can simplify code by reducing nesting.
  • Efficiency: Avoids executing unnecessary code for certain iterations.
  • Flexibility: Allows for handling specific cases within a loop without altering its overall structure.

Cons of Using 'continue'

  • Reduced Clarity: Overuse can make the loop's logic harder to follow.
  • Potential for Bugs: Misplaced continue statements can lead to unexpected behavior.
  • Maintenance Challenges: Complex continue conditions can be difficult to understand and modify.

FAQ

  • What's the difference between 'break' and 'continue'?

    break terminates the entire loop, while continue only skips the current iteration and proceeds to the next one. break exits the loop entirely, while continue jumps to the next iteration of the loop.
  • Can 'continue' be used inside a 'switch' statement?

    No, continue is designed to be used within loops (for, while, do-while) to skip iterations. Using it inside a switch statement will result in a compilation error.