C#: The continue statement

This entry is part 48 of 71 in the series C#

The continue statement is like break but it only stops the execution of the current iteration and causes control to begin with the next iteration.

For example, the following code prints the number 0 to 9, except 6.

using System;

namespace Conditional
{
    static class Program
    {
        static void Main()
        {
            for (int i = 0; i < 10; i++)
            {
                if (i == 6)
                {
                    continue;
                }
                Console.WriteLine(i);
            }
            Console.ReadLine();
        }
    }
}

When i is equals to 6, the expression of the if statement evaluates to true and causes the continue statement to be called.

As a result, the statement below it that prints the value of i is not executed and control continues with the next loop, i.e. for i equal to 7.

Series Navigation<< C#: The break statement
C#: The switch statement >>

Leave a comment

Leave a Reply