-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreakStatement.java
More file actions
34 lines (29 loc) · 957 Bytes
/
Copy pathBreakStatement.java
File metadata and controls
34 lines (29 loc) · 957 Bytes
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
package Phase1_CoreLanguage.DecisionMaking.JumpStatement;
/**In Java, break is majorly used for:
Terminate a sequence in a switch statement (discussed above).
To exit a loop.
Used as a “civilized” form of goto.
Using break to exit a Loop
Using break, we can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop.
Note: Break, when used inside a set of nested loops, will only break out of the innermost loop.**/
public class BreakStatement {
public static void main(String args[])
{
// Initially loop is set to run from 0-9
for (int i = 0; i < 10; i++)
{
// terminate loop when i is 5.
if (i == 5)
break;
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
//OUTPUT
//i: 0
//i: 1
//i: 2
//i: 3
//i: 4
//Loop complete.
}