Java break statement
In this chapter you will learn:
- When to use break statement
- Syntax for break statement
- Example - How to exit a for loop for a condition
- Example - break ut of a while loop
- 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
Description
When a break
statement is encountered inside a loop,
the loop is terminated and program control
resumes at the next statement following the loop.
Syntax
break;
or
break labelName;
Example
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);
}//w ww . j a va 2 s . co m
System.out.println("Loop complete.");
}
}
This program generates the following output:
Example 2
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;//ww w. ja 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:
Example 3
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 ww w. j a va2 s. c om
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:
Example 4
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 + " ");
}/*from ww w . java 2s. c o m*/
System.out.println();
}
System.out.println("Loops complete.");
}
}
This program generates the following output:
Example 5
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://from w w w.j a v a 2 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.
Example 6
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 w ww .j a v a2 s.com*/
continue outer;
}
System.out.print(" " + (i * j));
}
}
System.out.println();
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- What is Java continue statement
- Syntax for Java continue statement
- Example - Java continue statement
- How to use continue and label together