Saturday, August 14, 2010

Break: Buddy.java

Courtesy of education.oracle.com

Given: 

5. public class Buddy {
6.   public static void main(String[] args) {
7.     def:
8.     for(short s = 1; s < 7; s++) {
9.       if(s == 5) break def;
10.       if(s == 2) continue;
11.       System.out.print(s + ".");
12.     }
13.   }
14. }

What is the result?
a) 1.
b) 1.2.
c) 1.3.4.
d) 1.2.3.4.
e) 1.3.4.5.6.
f) 1.2.3.4.5.6.
g) Compilation fails. 


c is correct. The continue skips the current iteration, the break ends the entire loop.
 

No comments:

Post a Comment