-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathContinueExamples.java
59 lines (52 loc) · 1.36 KB
/
ContinueExamples.java
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
59
package loopAndFlow;
public class ContinueExamples {
public static void main(String[] args) {
// Continue statement skips rest of the statements in the loop
// and starts next iteration
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
System.out.print(i);
}
// Output is 012346789
// Not that the output does not contain 5
System.out.println();
// Continue can be used in a while also
int i = 0;
while (i < 10) {
i++;
if (i == 5) {
continue;
}
System.out.print(i);
}
// Output is 1234678910
// Not that the output does not contain 5
System.out.println();
// Continue statement takes execution to next iteration of inner most
// loop
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 10; k++) {
if (k == 5) {
continue;// skips to next iteration of k loop
}
System.out.print(j + "" + k);
}
}
// Output is 000102030406070809101112131416171819
// Not that the output does not contain 05,15
System.out.println();
// To get out of an outer for loop, label's need to be used
outer: for (int j = 0; j < 2; j++) {
for (int k = 0; k < 10; k++) {
if (k == 5) {
continue outer;// skips to next iteration of j loop
}
System.out.print(j + "" + k);
}
}
// Output is 00010203041011121314
// Not that the output does not contain anything after 05 and also 15
}
}