Selection and Iteration Statement Selection Table


break and continue

The break and continue statements are used to achieve fine-grained iteration statement control.

The break statement, as you have already learned, is used to exit a switch statement. It is also used to exit for, while, and do loops. There are two forms of break statement: unlabeled and labeled. This section will show you how to use both forms.

The continue statement is used to stop the processing of the current loop iteration and begin the next iteration. It is used in conjunction with for, while, and do loops. It has two forms as well: unlabeled and labeled.

Unlabeled break

An unlabeled break statement looks like this:

 break;

An unlabeled break statement is used in the body of for, while, and do statements to immediately exit the loop and proceed to the next statement. If used within a nested loop structure the unlabeled break statement will only exit its enclosing loop. Example 7.14 shows the unlabeled break statement in action. Figure 7-20 shows the results of running this program.

Example 7.14: BreakStatementTest.java

image from book
 1     public class BreakStatementTest { 2       public static void main(String[] args){ 3 4         for(int i = 0; i < 2; i++){ 5           for(int j = 0; j < 1000; j++){ 6           System.out.println("Inner for loop - j = " + j); 7           if(j == 3) break; 8         } 9         System.out.println("Outer for loop - i = " + i); 10       } 11     } 12    }
image from book

image from book
Figure 7-20: Results of Running Example 7.14

In this example an unlabeled break statement is used to exit a nested for loop. The inner for loop is set to loop 1000 times, however, an if statement on line 7 checks the value of j. If j == 3 the break statement executes, otherwise the loop is allowed to continue. As soon as j == 3 evaluates to true the inner for loop is terminated and the outer for loop executes the System.out.println() statement on line 9 and then begins another iteration.

Labeled break

The labeled break statement is kinda cool. It is used to break out of a particular outer or containing loop. To use a labeled break statement you must assign names to the iteration statement you wish to exit and use that name along with the break statement. Example 7.15 shows a labeled break statement in action. Figure 7-21 shows the results of running this program.

Example 7.15: BreakStatementTest.java (mod 1)

image from book
 1     public class BreakStatementTest { 2       public static void main(String[] args){ 3 4         Outer_Loop: for(int i = 0; i < 2; i++){ 5                        for(int j = 0; j < 1000; j++){ 6                          System.out.println("Inner for loop - j = " + j); 7                        if(j == 3) break Outer_Loop; 8                        } 9                     System.out.println("Outer for loop - i = " + i); 10                    } 11      } 12    }
image from book

image from book
Figure 7-21: Results of Running Example 7.15

Notice in example 7.15 how the outer for loop starting on line 4 is preceded by the label “Outer_Loop:”. Doing so makes the outer for statement a labeled statement. The break statement can now be used on line 7 with the name of the loop it wishes to exit, which in this case is Outer_Loop. Notice the difference between the output of this program shown in figure 7-21 and the output of the unlabeled break program shown in figure 7.20.

Unlabeled Continue

The unlabeled continue statement stops the current iteration of its containing loop and begins a new iteration. Example 7.16 shows the unlabeled continue statement in action in a short program that prints odd integers. Figure 7-22 shows this program in action.

Example 7.16: ContinueStatementTest.java

image from book
 1     public class ContinueStatementTest { 2       public static void main(String[] args){ 3          int limit_i = Integer.parseInt(args[0]); 4 5          for(int i = 0; i < limit_i; i++){ 6              if((i % 2) == 0) 7                  continue; 8              System.out.println(i); 9          } 10      } 11    }
image from book

image from book
Figure 7-22: Results of Running Example 7.16

Referring to example 7.16 — a string argument is entered on the command line, converted into an integer value, and assigned to the limit_i variable. The for statement uses the limit_i variable to determine how many loops it should perform. The if statement on line 6 uses the modulus operator % to see if the current loop index i is evenly divisible by 2. If so, it’s not an odd number and the continue statement is executed to force another iteration of the for loop. If the looping index i proves to be odd then the continue statement is bypassed and the remainder of the for loop executes resulting in the odd number being printed to the console.

Labeled Continue

The labeled continue statement is used to force a new iteration of a labeled iteration statement. It can be used with for, while and do statements just like its unlabeled counterpart. Example 7.17 shows the labeled continue statement in action.

Example 7.17: ContinueStatementTest.java (mod 1)

image from book
 1     public class ContinueStatementTest { 2       public static void main(String[] args){ 3          int inner_loop_limit = Integer.parseInt(args[0]); 4          int outer_loop_limit = Integer.parseInt(args[1]); 5          int outer_loop_counter = 0; 6          int inner_loop_counter = 0; 7 8        outer_loop: while(outer_loop_counter++ < outer_loop_limit){ 9                     while(inner_loop_counter++ < inner_loop_limit){ 10                      if((inner_loop_counter % 2) == 0) 11                        continue outer_loop; 12                    System.out.println(inner_loop_counter); 13                    } 14                   } 15      } 16    }
image from book

I admit — example 7.17 is a bit contrived but it does illustrate some good points. First, it calculates and prints odd numbers just like the previous example. It uses nested while loops whose loop limits are set via command line arguments. The variables outer_loop_counter and inner_loop_counter are incremented in the conditional expression of each while statement rather than being incremented in the body of each statement as shown earlier in the chapter. And although it is contrived it is a fun way to experiment with labeled continue statements.

image from book
Figure 7-23: Results of Running Example 7.17 with Different Loop Limits

Quick Review

The break and continue keywords provide fine-grained control over iteration statements. In addition to exiting switch statements, the break statement is used to exit for, while, and do loops. There are two forms of break statement: unlabeled and labeled. Unlabeled break exits its immediate containing loop; labeled break is used to exit a labeled iteration statement. The continue keyword terminates the current iteration statement loop and forces the start of a new iteration. Unlabeled continue will force a new iteration of its immediate containing loop; labeled continue will force a new iteration of a labeled iteration statement.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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