C# continue statement

In this chapter you will learn:

  1. What is continue statement
  2. How to create continue statement
  3. Example for continue statement
  4. continue within if 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;//w ww .  jav a 2 s . co  m

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;//ww  w.j a v a 2s. c om

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


    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is goto statement
  2. How to create goto statement
  3. Example for goto statement
  4. Use goto with a switch
Home »
  C# Tutorial »
    C# Language »
      C# Statements
C# Comments
C# if statement
C# while loop
C# do while loop
C# for loops
C# foreach statement
C# switch statement
C# break statement
C# continue statement
C# goto statement
C# XML Documentation