❮ Previous Chapter
Next Chapter ❯
❮ Previous Chapter Next Chapter ❯
In this chapter, we will discuss how to use the break keyword in for loop, while loop, switch-case statement, and in nested loops with examples.
In Java, the break statement has three uses. First, it terminates a statement sequence in a switch statement. Second, it can be used to exit a loop(for loop, while loop, etc). Third, it can be used as a “civilized” form of goto.
Table of contents
- Using a break to Exit a for Loop
- Using break to Exit a while Loop
- Using break with Nested Loops
- Using break with a switch case Statement
- Using break as a Civilized Form of goto
1. Using a break to Exit a for Loop
By using a break, you can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop. When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop.
Here is a simple example:
package net.javaguides.corejava.controlstatements.loops;
public class BreakWithForLoop {
public static void main(String args[]) {
for (int i = 0; i < 100; i++) {
if (i == 10)
break; // terminate loop if i is 10
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
Output:
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
Loop complete.
As we can see, although the for loop is designed to run from 0 to 99, the break statement causes it to terminate early, when i equals 10.
2. Using break to Exit a while Loop
package net.javaguides.corejava.controlstatements.loops;
public class BreakWithWhileLoop {
public static void main(String args[]) {
int i = 0;
while (i < 100) {
if (i == 10)
break; // terminate loop if i is 10
System.out.println("i: " + i);
i++;
}
System.out.println("Loop complete.");
}
}
Output:
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
Loop complete.
3. Using break with Nested Loops
When break keyword used inside a set of nested loops, the break statement will only break out of the innermost loop.
For example:
package net.javaguides.corejava.controlstatements.loops;
public class BreakWithNestedLoops {
public static void main(String args[]) {
for (int i = 0; i < 3; i++) {
System.out.print("Pass " + i + ": ");
for (int j = 0; j < 100; j++) {
if (j == 10)
break; // terminate loop if j is 10
System.out.print(j + " ");
}
System.out.println();
}
System.out.println("Loops complete.");
}
}
Output:
Pass 0: 0 1 2 3 4 5 6 7 8 9
Pass 1: 0 1 2 3 4 5 6 7 8 9
Pass 2: 0 1 2 3 4 5 6 7 8 9
Loops complete.
As we can see, the break statement in the inner loop only causes termination of that loop. The outer loop is unaffected.
4. Using break with a switch case Statement
public class BreakSwitchCaseExample {
public static void main(String args[]) {
int num = 2;
switch (num) {
case 1:
System.out.println("Case 1 ");
break;
case 2:
System.out.println("Case 2 ");
break;
case 3:
System.out.println("Case 3 ");
break;
default:
System.out.println("Default ");
}
}
}
Output:
Case 2
5. Using break as a Civilized Form of goto
We can use a break statement with a label. This feature is introduced since JDK 1.5. So, we can break any loop in Java now whether it is an outer loop or inner.
package net.javaguides.corejava.controlstatements.loops;
public class BreakGoto {
public static void main(String args[]) {
boolean t = true;
first: {
second: {
third: {
System.out.println("Before the break.");
if (t)
break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}
Output:
Before the break.
This is after second block.
❮ Previous Chapter Next Chapter ❯
Comments
Post a Comment
Leave Comment