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

Inside the loop

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

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

Inside the loop

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

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

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

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

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