Continue in C#

The continue statement is used to skip the current iteration and move to the next iteration of the loop, It is used within loops, such as for, for each, while, and do while.

Syntax

continue;

Example 1. Continue in For Loop

Skip odd numbers and print only even numbers.

for (int i = 1; i <= 10; i++)
{
    if (i % 2 != 0)
    {
        continue; // skip the iteration and print even numbers only
    }
    Console.WriteLine(i);
}

Process flow

  • Initialization: The loop starts with int i = 1.
  • Condition Check: The loop runs as long as i is less than or equal to 10.
  • Increment: After each iteration, i is incremented by 1.

Inside the loop

  • The condition if (i % 2 != 0) checks if i is an odd number.
  • If i is odd (i % 2 != 0 evaluates to true), the continue statement is executed, skipping the rest of the loop body and proceeding to the next iteration.
  • If I is even, the Console.WriteLine(i) statement is executed, printing the value of i.

Explanation

Iteration 1: i = 1 (odd), continue is executed, skipping Console.WriteLine.
Iteration 2: i = 2 (even), Console.WriteLine(2) is executed.
Iteration 3: i = 3 (odd), continue is executed, skipping Console.WriteLine.
Iteration 4: i = 4 (even), Console.WriteLine(4) is executed.
Iteration 5: i = 5 (odd), continue is executed, skipping Console.WriteLine.
Iteration 6: i = 6 (even), Console.WriteLine(6) is executed.
Iteration 7: i = 7 (odd), continue is executed, skipping Console.WriteLine.
Iteration 8: i = 8 (even), Console.WriteLine(8) is executed.
Iteration 9: i = 9 (odd), continue is executed, skipping Console.WriteLine.
Iteration 10: i = 10 (even), Console.WriteLine(10) is executed.

This pattern continues until I reach 10, ensuring that only even numbers are printed.

Result

  • 2
  • 4
  • 6
  • 8
  • 10

Example 2. Continue in Foreach Loop

Skip odd numbers and print only even numbers.

int[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

foreach (int number in numbers)
{
    if (number % 2 != 0) // Skip odd numbers
    {
        continue;
    }
    Console.WriteLine(number); // This will only print even numbers
}

Process flow

  • Array Initialization: An array number is initialized with values from 0 to 10.
  • Iteration: The foreach loop iterates over each element in the numbers array.

Inside the loop

  • The condition if (number % 2 != 0) checks if the current number is odd.
  • If the number is odd (number % 2 != 0 evaluates to true), the continue statement is executed, skipping the rest of the loop body and proceeding to the next iteration.
  • If the number is even, the Console.WriteLine(number) statement is executed, printing the value of a number.

Explanation

Iteration 1: number = 0 (even), Console.WriteLine(0) is executed.
Iteration 2: number = 1 (odd), continue is executed, skipping Console.WriteLine.
Iteration 3: number = 2 (even), Console.WriteLine(2) is executed.
Iteration 4: number = 3 (odd), continue is executed, skipping Console.WriteLine.
Iteration 5: number = 4 (even), Console.WriteLine(4) is executed.
Iteration 6: number = 5 (odd), continue is executed, skipping Console.WriteLine.
Iteration 7: number = 6 (even), Console.WriteLine(6) is executed.
Iteration 8: number = 7 (odd), continue is executed, skipping Console.WriteLine.
Iteration 9: number = 8 (even), Console.WriteLine(8) is executed.
Iteration 10: number = 9 (odd), continue is executed, skipping Console.WriteLine.
Iteration 11: number = 10 (even), Console.WriteLine(10) is executed.

This pattern continues for each element in the array, ensuring that only even numbers are printed.

Result

  • 0
  • 2
  • 4
  • 6
  • 8
  • 10

Example 3. Continue in While Loop

Skip odd numbers and print only even numbers.

int i = 0;
while (i <= 10)
{
    if (i % 2 != 0) // Check if the number is odd
    {
        i++; // Increment i
        continue; // Skip the rest of the loop body
    }

    Console.WriteLine(i);
    i++; // Increment i
}

Process flow

  • Initialization: Start with an index variable i set to 0.
  • Condition Check: The loop runs as long as i is less than or equal to 10.
  • Inside the Loop: Check if i is odd using the condition if (i % 2 != 0).
    1. If the condition is true (i.e., i is odd), increment i and use the continue statement to skip the rest of the loop body, moving to the next iteration.
    2. If the condition is false (i.e., i is even), print the value of i and then increment i.

Explanation

Iteration 1: i = 0 (even), Console.WriteLine(0) is executed, i is incremented to 1.
Iteration 2: i = 1 (odd), i is incremented to 2, continue is executed, skipping Console.WriteLine.
Iteration 3: i = 2 (even), Console.WriteLine(2) is executed, i is incremented to 3.
Iteration 4: i = 3 (odd), i is incremented to 4, continue is executed, skipping Console.WriteLine.
Iteration 5: i = 4 (even), Console.WriteLine(4) is executed, i is incremented to 5.
Iteration 6: i = 5 (odd), i is incremented to 6, continue is executed, skipping Console.WriteLine.
Iteration 7: i = 6 (even), Console.WriteLine(6) is executed, i is incremented to 7.
Iteration 8: i = 7 (odd), i is incremented to 8, continue is executed, skipping Console.WriteLine.
Iteration 9: i = 8 (even), Console.WriteLine(8) is executed, i is incremented to 9.
Iteration 10: i = 9 (odd), i is incremented to 10, continue is executed, skipping Console.WriteLine.
Iteration 11: i = 10 (even), Console.WriteLine(10) is executed, i is incremented to 11.

Once I reach 11, the condition I <= 10 becomes false, and the loop exits. This ensures that only even numbers from 0 to 10 are printed.

Result

  • 0
  • 2
  • 4
  • 6
  • 8
  • 10

Example 4. Continue in do While Loop

Skip odd numbers and print only even numbers.

int i = 0;
do
{
    if (i % 2 != 0) // Check if the number is odd
    {
        i++; // Increment i
        continue; // Skip the rest of the loop body
    }

    Console.WriteLine(i); // Print the even number
    i++; // Increment i
} while (i <= 10);

Process flow

  • Initialization: Start with an index variable I set to 0.
  • Condition Check: The loop runs as long as i is less than or equal to 10.
  • Inside the Loop: Check if i is odd using the condition if (i % 2 != 0).
    1. If the condition is true (i.e., i is odd), increment i and use the continue statement to skip the rest of the loop body, moving to the next iteration.
    2. If the condition is false (i.e., i is even), print the value of i and then increment i.

Explanation

Iteration 1: i = 0 (even), Console.WriteLine(0) is executed, i is incremented to 1.
Iteration 2: i = 1 (odd), i is incremented to 2, continue is executed, skipping Console.WriteLine.
Iteration 3: i = 2 (even), Console.WriteLine(2) is executed, i is incremented to 3.
Iteration 4: i = 3 (odd), i is incremented to 4, continue is executed, skipping Console.WriteLine.
Iteration 5: i = 4 (even), Console.WriteLine(4) is executed, i is incremented to 5.
Iteration 6: i = 5 (odd), i is incremented to 6, continue is executed, skipping Console.WriteLine.
Iteration 7: i = 6 (even), Console.WriteLine(6) is executed, i is incremented to 7.
Iteration 8: i = 7 (odd), i is incremented to 8, continue is executed, skipping Console.WriteLine.
Iteration 9: i = 8 (even), Console.WriteLine(8) is executed, i is incremented to 9.
Iteration 10: i = 9 (odd), i is incremented to 10, continue is executed, skipping Console.WriteLine.
Iteration 11: i = 10 (even), Console.WriteLine(10) is executed, i is incremented to 11.

Once I reach 11, the condition I <= 10 becomes false, and the loop exits. This ensures that only even numbers from 0 to 10 are printed.

Result

  • 0
  • 2
  • 4
  • 6
  • 8
  • 10


Similar Articles