-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsJavaSupportGoto.java
More file actions
66 lines (53 loc) · 1.93 KB
/
Copy pathIsJavaSupportGoto.java
File metadata and controls
66 lines (53 loc) · 1.93 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
66
package Phase1_CoreLanguage.Extra;
/**
* Java does not support goto, it is reserved as a keyword just in case they wanted to add it to a later version.
* Unlike C/C++, Java does not have goto statement, but java supports label.
* The only place where a label is useful in Java is right before nested loop statements.
* We can specify label name with break to break out a specific outer loop.
* Similarly, label name can be specified with continue.**/
public class IsJavaSupportGoto {
public static void main(String[] args) {
IsJavaSupportGoto isJavaSupportGoto = new IsJavaSupportGoto();
isJavaSupportGoto.usingBreak();
isJavaSupportGoto.usingContinue();
}
//Using break with label in Java
private void usingBreak() {
// label for outer loop
outer:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 1)
break outer;
System.out.println(" value of j = " + j);
}
}
// OUTPUT
// value of j = 0
}
//Using continue with label in Java
private void usingContinue() {
// label for outer loop
outer:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 1)
continue outer;
System.out.println(" value of j = " + j);
}
}
//OUTPUT
//value of j = 0
//value of j = 0
//value of j = 0
//value of j = 0
//value of j = 0
//value of j = 0
//value of j = 0
//value of j = 0
//value of j = 0
//value of j = 0
//Since continue statement skips to the next iteration in the loop, it iterates for 10 times as i iterates from 0 to 9.
// So the outer loop executes for 10 times and the inner for loop executes 1 time in each of the outer loop.
}
}