C# continue statement

Description

The continue statement forgoes the remaining statements in a loop and makes an early start on the next iteration.

Syntax

continue statement has the following syntax.


continue;

Example

The following code only prints out 6,7,8,9,10.


using System;/*from ww w  .  j a  v a  2s . c  om*/

class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i <= 10; i++)
        {
            if (i <= 5)
            {
                continue;
            }
            Console.WriteLine(i);

        }

    }
}

The output:

Example 2

The following code combines the modular operator and continue statement to print out odd numbers only.


using System;//  www  .j  a  va 2 s.  c o m

class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i < 20; i++)
        {
            if (i % 2 == 0)
            {
                continue;
            }
            Console.WriteLine(i);
        }


    }
}

The output:





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception