CS Continue Statement

Continuing is also a statement of loop control, just like the Break Statement. Continue statement is opposite to the break statement, instead of terminating the loop, it forces to perform the next loop iteration. As the name implies, the continues statement force the loop to continue or perform the next iteration.

If the continuation statement in the loop is executed, the code inside the loop after the continuous statement is omitted and the next iteration of the loop begins.

The C# continue statement is used to keep the loop going means a current program flow continues and skip the remaining code in a certain condition. In the case of an internal loop, only an internal loop is continued.

Syntax


    jump-statement;    
    continue;
  

Example:


using System;
public class ContinueExample
{
    public static void Main(string[] args)
    {
        for (int i = 1; i <= 10; i++)
        {
            if (i == 5)
            {
                continue;
            }
            Console.WriteLine(i);
        }
    }
}

Output:

1
2
3
4
6
7
8
9
10

 

 

C# Continue statement only continues the internal loop if we use the continuous statement within the internal loop.

Example:


using System;
public class ContinueExample
{
    public 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);
            }
        }
    }
}

Output:

1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3