-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25_Label.java
More file actions
65 lines (50 loc) · 1.96 KB
/
25_Label.java
File metadata and controls
65 lines (50 loc) · 1.96 KB
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
60
61
62
63
64
65
import java.util.*;
class Solution {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// By default break; cuts out of the nearest block.
// But if we use labelled break, it redirects the control to the
// label statement.
// Rules for labels:
// We can only give labels to loops and break out of it.
outer:
for(int i = 0; i < 10; i++) {
inner:
for(int j = 0; j < 10; ++j){
// Skip the iteration when i == 3 and resume from 4;
if (i == 3) break;
// skip the iteration when i == 5 and break out of inner loop;
if (i == 5) break inner;
// skip any iteration when i == 7 and break out of outer loop.
if (i == 7) break outer;
System.out.print(i + j + " ");
}
System.out.println();
}
System.out.println("We are out of outer loop");
}
}
class Solution1 {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// labelled Continue; Unlabelled Continue;
// Rules for labels:
// We can only give labels to loops and break out of it.
// Same labels can be used with continue also;
outer:
for(int i = 0; i < 10; i++) {
inner:
for(int j = 0; j < 10; ++j){
// Skip the iteration when j == 3 and resume from 4;
if (j == 3) continue;
// skip the iteration when i == 5 and continue inner loop for i == 6;
if (i == 5) continue inner;
// skip any iteration when i == 7 and continue inner loop for i == 8;
if (i == 7) continue outer;
System.out.print( "("+ i + ", " + j + ")" + " ");
}
System.out.println();
}
System.out.println("We are out of outer loop");
}
}