continue statement

In this chapter you will learn:

  1. How to use continue statement
  2. continue within if statement

Use continue statement

continue statement restarts the loop iteration.

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

using System;/*from   j a  v  a 2  s . c o  m*/

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

        }

    }
}

The output:

continue within if statement

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

using System;/*from j a va2 s  .  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. How to use goto statement
  2. Use goto with a switch
Home » C# Tutorial » Statements
Comments
if Statement
while loop
do...while loop
for loop
foreach
switch
break
continue statement
goto statement
Xml Documentation