Java break statement
In this chapter you will learn:
- How to exit a for loop for a condition
- How to use break statement to exit an infinite loop
- How to break just one layer of the nested loop
- How to break from a switch statement
- How to break where you want
Exit a 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:
public class Main {
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);
}//from java 2 s.co m
System.out.println("Loop complete.");
}
}
This program generates the following output:
The break
statement can be used with while
loop as well.
For example, here is the preceding program coded by use of a while
loop.
public class Main {
public static void main(String args[]) {
int i = 0;// j a v a 2 s . com
while (i < 100) {
if (i == 10)
break; // terminate loop if i is 10
System.out.println("i: " + i);
i++;
}
System.out.println("Loop complete.");
}
}
The output:
Exit an infinite loop
The break
statement is useful to exit an infinite loop.
In the following while
loop the true
value is hard coded in, therefore
the while
loop is an infinite loop. Then it uses an if
statement combined with
the break
statement to exit the whole
loop when i
is 10.
public class Main {
public static void main(String args[]) {
int i = 0;/*from j a v a 2s . c o m*/
while (true) {
if (i == 10){
break; // terminate loop if i is 10
}
System.out.println("i: " + i);
i++;
}
System.out.println("Loop complete.");
}
}
The output:
break from a nested loop
When used inside a set of nested loops, the break
statement will only break out of
the inner-most loop. For example:
public class Main {
public static void main(String args[]) {
for (int i = 0; i < 5; 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 + " ");
}/* j av a 2s .co m*/
System.out.println();
}
System.out.println("Loops complete.");
}
}
This program generates the following output:
break from switch branch
The break
that terminates a switch
statement affects only that
switch
statement and not any enclosing loops.
public class Main {
public static void main(String args[]) {
for (int i = 0; i < 6; i++)
switch (i) {
case 1:/* j a v a2 s .c o m*/
System.out.println("i is one.");
for (int j = 0; j < 5; j++) {
System.out.println("j is " + j);
}
break;
case 2:
System.out.println("i is two.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}
The output:
From the result we can see that the break
statement only exit the
switch
statement.
break with a label
We can specify a label
for break
statement and let the code logic exit to that
point.
The following code uses the label
to make break statement exit two layers of the nested
for
loop.
public class Main {
public static void main(String args[]) {
outer: for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j + 1 < i) {
System.out.println();/*from j a va 2s . co m*/
continue outer;
}
System.out.print(" " + (i * j));
}
}
System.out.println();
}
}
The output:
Next chapter...
What you will learn in the next chapter: