-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLabel_break_statement.java
59 lines (52 loc) · 1.75 KB
/
Label_break_statement.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
/*
the label identifier to specify the outer loop. Now, notice how the break statement is used (break label;).
Here, the break statement is terminating the labeled statement (i.e. outer loop).
Then, the control of the program jumps to the statement after the labeled statement.
Here's another example:
while (testExpression) {
// codes
second:
while (testExpression) {
// codes
while(testExpression) {
// codes
break second;
}
}
// control jumps here
}
In the above example, when the statement break second; is executed, the while loop labeled as second is terminated.
And, the control of the program moves to the statement after the second while loop.
*/
public class Label_break_statement
{
public static void main(String[] args)
{
// for loop is labeled as first
first:
for (int i = 1; i < 5; i++)
{
// for loop loop is labeled as second
second:
for (int j = 1; j < 3; j++)
{
System.out.println("i = " + i + "; j = " + j);
//the break statement breaks the first for loop
if (i==2)
break first;
/*
In the above example, the labeled break statement is used to terminate the loop labeled as first.
That is,
first:
for(int i = 1; i < 5; i++) {...}
Here, if we change the statement break first; to break second;
the program will behave differently.
In this case, for loop labeled as second will be terminated
*/
}
}
}
}
/*
Note: The break statement is also used to terminate cases inside the switch statement
*/