C# Continue Statement

C# continue statement is used to terminate the current iteration and start the next iteration in C# Loop statement.

Diagram:

C# continue statement diagram

  • The above diagram describes how a continue statement runs.
  • The loop runs and meets a continue statement.
  • The continue statement stops the current iteration and control goes back to the beginning of the loop to start the next iteration.
  • The continue statement is mostly used in for loop statement or foreach statement.

Example 01-36-01

In this example, we'll output all even numbers which is less than or equal 20.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;

namespace ContinueSimple
{
    class Program
    {
        static void Main(string[] args)
        {
            const int MaxRun = 20;

            for(int i=2; i <= MaxRun; i++){
                if( i % 2 == 1 ){
                    continue;
                }
                Console.Write("{0} ", i);
            }
            Console.WriteLine();

            Console.Read();
        }
    }
}

Output

2 4 6 8 10 12 14 16 18 20 
  • Line 9: Declare the maximum output number.
  • Line 11-16: For loop and i starts with 2 and it is no more than 20.
  • Line 12-14: If i is an odd number, we'll go back to the next iteration. That means the control goes to i++ and then check if i <= MaxRun to start a new cycle.
  • Line 15: Otherwise we'll output the even number.
  • Line 17: Output a new line.

Diagram:

C# Continue Statement in a Nested Loops

  • There are 2 loops in the above diagram. One is called outer loop and the other is called inner loop.
  • The continue statement in green is in the inner loop and when it runs, the control will go back to Inner Loop Start in green to start a new iteration.
  • In the outer loop, there is another break statement in red. After it runs, the control will go back to the Outer Loop Start in red to start a new iteration in outer loop statement.

Example 01-36-02

The following example will output 1-9 times table excluding 5 number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;

namespace ContinueNested
{
    class Program
    {
        static void Main(string[] args)
        {
            const int Max = 10;

            for(int i=1; i<Max; i++){
                if ( i == 5 ) continue;
                for(int j=1; j<Max; j++){
                    if ( j == 5 ) continue;
                    Console.Write("{0}x{1}={2} ", i, j, i*j);
                }
                Console.WriteLine();
            }
            Console.Read();
        }
    }
}

Output

1x1=1 1x2=2 1x3=3 1x4=4 1x6=6 1x7=7 1x8=8 1x9=9 
2x1=2 2x2=4 2x3=6 2x4=8 2x6=12 2x7=14 2x8=16 2x9=18 
3x1=3 3x2=6 3x3=9 3x4=12 3x6=18 3x7=21 3x8=24 3x9=27 
4x1=4 4x2=8 4x3=12 4x4=16 4x6=24 4x7=28 4x8=32 4x9=36 
6x1=6 6x2=12 6x3=18 6x4=24 6x6=36 6x7=42 6x8=48 6x9=54 
7x1=7 7x2=14 7x3=21 7x4=28 7x6=42 7x7=49 7x8=56 7x9=63 
8x1=8 8x2=16 8x3=24 8x4=32 8x6=48 8x7=56 8x8=64 8x9=72 
9x1=9 9x2=18 9x3=27 9x4=36 9x6=54 9x7=63 9x8=72 9x9=81 
  • Line 9: Declare an int constant to hold the maximum times to loop.
  • Line 11: Start outer for loop and define an int variable i.
  • Line 12: If i==5 return true, the control will go back to line 11. Then i++ will run and start the next outer loop iteration.
  • Line 13: Start inner for loop and define an int variable j.
  • Line 14: If j==5 return true, the control will go back to line 13. Then j++ will run and start the next inner loop iteration.
  • Line 15: Output i times j result.
  • Line 16: End of the inner loop.
  • Line 17: Output a new line.
  • Line 18: End of the outer loop.
  • You can see the above result, no 5 times others or other number times 5 is outputted.