The continue Statement

A continue statement may be used to skip the remaining portion of a loop. In a while or do-while loop, the continue statement transfers execution to the evaluation of the conditional expression; whence normal loop processing resumes. In the case of a for loop, the iteration expression of the loop is evaluated, the conditional expression is evaluated and the loop then continues as normal. For example, to add the numbers from 0 to 9, excluding 5, the following program can be used.

// Continue -- Using continue

using System;

class Program
    static void Main()
        int j = 0;
        for (int i = 0; i < 10; i++)
            if (i == 5) continue;
            j += i;
        Console.WriteLine("The sum is {0}", j);

The output of the program is shown below.

Sum = 40

The above program merely skips 5 when adding 0 through to 9. The continue obviously differs from the break in that for continue, the loop continues to operate, it merely skips the remainder of the current iteration (whereas for break the loop terminates altogether). A continue statement can be applied to a for loop, a while loop and a do-while loop.