C# Break Statement

C# break statement is always used to terminate the current control flow of loop statements or switch statement and start running the code just follows the terminated statement. For the example of break statement running in a switch statement, you can check C# switch for details.

Diagram:

C# break statement diagram

  • The above is the break statement diagram which describes what happened when we run a break statement.
  • The loop keeps running the statements as usual until meeting a break statement.
  • break statement makes the control deliver to the next statement following the loop end.

Example 01-35-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;

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

            for(int i=0; i < MaxRun; i++){
                if( i > 5 ){
                    break;
                }
                Console.Write("{0} ", i);
            }
            Console.WriteLine();

            int j = 0;
            while( j < MaxRun ){
                if( j > 5 ){
                    break;
                }
                Console.Write("{0} ", j);
                j++;
            }
            Console.WriteLine();

            Console.Read();
        }
    }
}

Output

0 1 2 3 4 5 
0 1 2 3 4 5 
  • Line 11-16: This is a For loop statement which is planned to loop 10 times.
  • Line 12-14: This is a If statement. If i > 5, break statement will be run.
  • Line 15: Output from 0 to 5. When i = 6, the condition i>5 returns true then run break statement.
  • Line 17: Output a new line. The control goes here after break.
  • Line 20-26: This is a while statement. It is the same as the previous one. When i=6, we'll run the break statement.
  • Line 27: Output a new line.

Diagram:

C# Break 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 break statement in green is in the inner loop and after it runs, the control will go to the Next Statement in green.
  • In the outer loop, there is another break statement in red. After it runs, the control will go to the Next Statement in red.

Example 01-35-02

We know a prime number is a natural number which is greater than 1 and has no positive divisors except 1 and itself. The following example will try to find the biggest prime number under 1000.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;

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

            int i = Max - 1;
            while(i > 1){
                int j, m = i/2;
                for( j = 3; j <= m; j += 2 ){
                    if( i % j == 0 ){
                        break;
                    }
                }
                if( j > m ){
                    Console.WriteLine("The prime is {0}.", i);
                    break;
                }
                i -= 2;
            }

            Console.Read();
        }
    }
}

Output

The prime is 997.
  • Line 9: Declare an int constant Max = 1000.
  • Line 11: The prime number check starts 999.
  • Line 12-24: Outer loop to check each number from 999 and greater than 1.
  • Line 13: Declare 2 int variables. m stands for the maximum divisor which should no more than i/2.
  • Line 14-18: Inner loop. We'll try to find a divisor from 3 to m. Because i is an odd number, the divisor cannot be an even number. Each loop, j increments by 2.
  • Line 15-17: The condition returning true means i is not a prime so we use break statement to jump out the inner loop then get the next number to check.
  • Line 19: j > m means we cannot find a divisor in the inner loop, i.e. the inner loop was over normally because j <= m returns false. In this case, the prime number is found.
  • Line 20: Output the prime number.
  • Line 21: break to get out of the outer loop to run the statement in line 26.
  • Line 23: Otherwise (j<=m) we will get the next candidate number to start a new cycle.