Given:
A. 23
B. 234
C. 235
D. 2345
E. 2357
F. 23457
G. Compilation fails.
3. public class Breaker {
4. static String o = "";
5. public static void main(String[] args) {
6. z:
7. o = o + 2;
8. for(int x = 3; x < 8; x++) {
9. if(x==4) break;
10. if(x==6) break z;
11. o = o + x;
12. }
13. System.out.println(o);
14. }
15. }
What is the result?A. 23
B. 234
C. 235
D. 2345
E. 2357
F. 23457
G. Compilation fails.
why does this happened?
ReplyDeleteIts because java does not allow you to jump from inside a loop to another statement.
ReplyDeletewhile you jump out of a loop to a label. the label should be immediately above the loop and no other statement should should come in between.
The program will work if you put the label "z:" after the "o = o + 2;" statement.