Java Reference
In-Depth Information
10. System.out.println(“Working on “ + day);
11. } else {
12. break;
13. }
14. }
15. }
16.
17. public static void main(String [] args) {
18. Vacation v = new Vacation();
19. v.workUntil(Days.THURSDAY);
20. }
21.}
The enhanced for loop on line 8 iterates through the values of the enum and displays
a message on line 10 if the dayOff argument doesn't match the current day . Once line 9 is
false , the break occurs on line 12 and the loop terminates. The number of times this loop
iterates varies depending on the value of dayOff . The main method invokes workUntil with
Days.THURDAY as the argument, so the output of main is
Working on SUNDAY
Working on MONDAY
Working on TUESDAY
Working on WEDNESDAY
A break statement can contain a label denoting which control structure to break out of.
An unlabeled break statement terminates the immediately enclosing control structure. If
you need to break out of an outer loop or switch, you need to use a labeled break .
A label is a prefi x that appears before a statement and is followed by a colon:
label_name : statement
A label can be any valid identifi er, as long as it is does not hide a label being used by an
enclosing statement. The following while loop contains a label named myloop and a break
statement that refers to the myloop label. See if you can determine the output:
4. int count = 1;
5. int sum = 0;
6. myloop : while(count <= 100) {
7. sum += count++;
8. if(sum > 10) {
9. break myloop;
10. }
11.}
12.System.out.println(“sum = “ + sum);
13.System.out.println(“count = “ + count);
Search WWH ::




Custom Search