C# Continue Statement

C# Continue Statement

The C# jump statements are break, continue, goto, return, and throw.

1 - C# Continue Statement

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

The following loop skips even numbers:

for (int i = 0; i < 10; i++) { 
    if ((i % 2) == 0){
        continue; // continue with next iteration
    }
    Console.Write (i + " "); 
}  

Output: 

1 3 5 7 9

Syntax of C# Continue Statement:

The syntax for a continue statement in C# is as follows:

continue;

 

C# Continue Statement Flow Chart:

C# Continue Statement Flow Chart - XDevSpace

 

2 - C# Continue Statement Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            /* local variable definition */
            int a = 10;

            /* do loop execution */
            do
            {
                if (a == 15)
                {
                    /* skip the iteration */
                    a = a + 1;
                    continue;
                }
                Console.WriteLine("value of a: {0}", a);
                a++;
            }
            while (a < 20);
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

When the above code is compiled and executed, it produces the following result:

C# Continue Statement Example - XDevSpace

 

2.1 C# Continue Statement Example 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 10; i++)
            {
                if (i == 5)
                {
                    continue;
                }
                Console.WriteLine(i);
            }
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# Continue Statement Example

2.2 C# Continue Statement Example 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 4; i++)
            {
                if (i == 3)
                    continue;
                Console.WriteLine("i value: {0}", i);
            }
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# Continue Statement Example 2

 

3 - C# Continue Statement with Inner Loop

C# Continue Statement continues inner loop only if you use continue statement inside the inner loop.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 3; i++)
            {
                for (int j = 1; j <= 3; j++)
                {
                    if (i == 2 && j == 2)
                    {
                        continue;
                    }
                    Console.WriteLine(i + " " + j);
                }
            }
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

When the above code is compiled and executed, it produces the following result:

C# Continue Statement with Inner Loop - XDevSpace

 

4 - C# For Loop with Continue Statement

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 4; i++)
            {
                if (i == 3)
                    continue;
                Console.WriteLine("i value: {0}", i);
            }
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# For Loop with Continue Statement - XDevSpace

 

5 - C# While Loop with Continue Statement

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            while (i < 4)
            {
                i++;
                if (i == 2)
                    continue;
                Console.WriteLine("i value: {0}", i);
            }
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# While Loop with Continue Statement - XDevSpace

 

6 - C# Do-While Loop with Continue Statement

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            do
            {
                Console.WriteLine("i value: {0}", i);
                i++;
                if (i == 2)
                    continue;
            } while (i < 4);
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# While Loop with Continue Statement - XDevSpace