-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathessay6.txt
58 lines (54 loc) · 1.5 KB
/
essay6.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Java Essay Serials 1 - Java Basics - 6, How a Java program jump out a nested loop?
Within a loop statement, such as for,while and do-while statement, a break statement will terminate the loop execution and jump to the next statement following the loop.
for exmaple:
int m=0;
while(m<5){
if(m==2){
break;
}
System.out.println(m);
m++;
}
System.out.println("terminate while statement.");
the above code snippet will output:
0
1
terminate while statement.
with nested loop statements, the break statement only jump out the inner loop.
for example:
for (int n=0;n<3;n++) {
int m=0;
while(m<5){
if(m==2) break;
System.out.println(n+"-"+m);
m++;
}
System.out.println("terminate while statement.");
}
the above code snippet will output the following:
0-0
0-1
terminate while statement.
1-0
1-1
terminate while statement.
2-0
2-1
terminate while statement.
It apparently shows that the break statement only jump out the inner while loop.
To jump out a nested loop, you can use break with a label, for example:
Label1:
for (int n=0;n<3;n++) {
int m=0;
while(m<5){
if(m==2) break Label1;
System.out.println(n+"-"+m);
m++;
}
System.out.println("terminate while statement.");
}
System.out.println("terminate for loop.");
The running result will be:
0-0
0-1
terminate for loop.