Using break to Exit a Loop


It is possible to force an immediate exit from a loop, bypassing any code remaining in the body of the loop and the loop’s conditional test, by using the break statement. When a break statement is encountered inside a loop, the loop is terminated, and program control resumes at the next statement following the loop. Here is a simple example:

 // Using break to exit a loop. using System; class BreakDemo {   public static void Main() {     // use break to exit this loop     for(int i=-10; i <= 10; i++) {       if(i > 0) break; // terminate loop when i is positive       Console.Write(i + " ");     }     Console.WriteLine("Done");   } }

This program generates the following output:

 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 Done

As you can see, although the for loop is designed to run from 10 to 10, the break statement causes it to terminate early, when i becomes positive.

The break statement can be used with any of C#’s loops. For example, here is the previous program recoded to use a do-while loop:

 // Using break to exit a do-while loop. using System; class BreakDemo2 {   public static void Main() {     int i;     i = -10;     do {       if(i > 0) break;       Console.Write(i + " ");       i++;     } while(i <= 10);     Console.WriteLine("Done");   } }

Here is a more practical example of break. This program finds the smallest factor of a number.

 // Find the smallest factor of a value. using System; class FindSmallestFactor {   public static void Main() {     int factor = 1;     int num = 1000;     for(int i=2; i < num/2; i++) {       if((num%i) == 0) {         factor = i;         break; // stop loop when factor is found       }     }     Console.WriteLine("Smallest factor is " + factor);   } }

The output is shown here:

 Smallest factor is 2

The break stops the for loop as soon as a factor is found. The use of break in this situation prevents the loop from trying any other values once a factor has been found, thus preventing inefficiency.

When used inside a set of nested loops, the break statement will break out of only the innermost loop. For example:

 // Using break with nested loops. using System; class BreakNested {   public static void Main() {     for(int i=0; i<3; i++) {       Console.WriteLine("Outer loop count: " + i);       Console.Write("    Inner loop count: ");       int t = 0;       while(t < 100) {         if(t == 10) break; // terminate loop if t is 10         Console.Write(t + " ");         t++;       }       Console.WriteLine();     }     Console.WriteLine("Loops complete.");   } }

This program generates the following output:

 Outer loop count: 0     Inner loop count: 0 1 2 3 4 5 6 7 8 9 Outer loop count: 1     Inner loop count: 0 1 2 3 4 5 6 7 8 9 Outer loop count: 2     Inner loop count: 0 1 2 3 4 5 6 7 8 9 Loops complete.

As you can see, the break statement in the inner loop causes only the termination of that loop. The outer loop is unaffected.

Here are two other points to remember about break. First, more than one break statement may appear in a loop. However, be careful. Too many break statements have the tendency to destructure your code. Second, the break that terminates a switch statement affects only that switch statement and not any enclosing loops.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net