2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107 4. Labelled breaks breaks out of several levels of nested loops inside a pair of curly braces. public class Main {
public static void main(String args[]) {
int len = 100;
int key = 50;
int k = 0;
out: {
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if (i == key) {
break out;
}
k += 1;
}
}
}
System.out.println(k);
}
}
5.The Labeled break Statement
- The break statement can be followed by a label.
- The presence of a label will transfer control to the start of the code identified by the label.
- For example, consider this code.
|
public class MainClass {
public static void main(String[] args) {
OuterLoop: for (int i = 2;; i++) {
for (int j = 2; j < i; j++) {
if (i % j == 0) {
continue OuterLoop;
}
}
System.out.println(i);
if (i == 37) {
break OuterLoop;
}
}
}
}
|
|
2
3
5
7
11
13
17
19
23
29
31
37 http://www.java2s.com/Tutorial/Java/0080__ Statement-Control/Catalog0080__Statement-Control.htm |
|
No comments:
Post a Comment