Tuesday, August 24, 2010

Break: Breakfast.java

Courtesy of Sun Web Learning Center


Given:

 4. class Breakfast {
 5.   public static void main(String[] args) {
 6.     free:
 7.     for(int j = 3; j > 0; j--) {
 8.       for(int k = 0; k < 4; k++) {
 9.         if(j == 1) break free;
10.         if(k == 2) break;
11.         System.out.print(" " + j + k);
12.       }
13.     }
14.   }
15. }
What is the result?


A 30 31 20 21
B 30 31 32 20 21 22
C 30 31 32 33 20 21 22 23
D 30 31 32 20 21 22 10 11 12
E 30 31 32 33 20 21 22 23 10 11 12 13
F Compilation fails.

A is correct. The break halts the inner loop, the labeled break halts the outer loop.

No comments:

Post a Comment