Break. A loop continues until a condition is reached. And control falls through each case in a switch. With break, a keyword, we stop control flow.
It stops the entire loop. No further iterations in a loop will occur after a break is reached. Continue, meanwhile, will stop just the current iteration.
For-loop break. This program uses the break statement in a for-loop. We loop over 100 numbers from 0 to 99. But when 5 is reached, a break statement executes.
And The loop terminates before the value 6 is printed. The break is a way to stop a loop, eve none that has not reached its bounds.
Note The break statement has no arguments. We cannot use labels with it. We cannot use it to break out of nested loops at once.
Return A return sometimes is used in place of a break (but the enclosing method returns also).
public class Program {
public static void main(String[] args) {
// Loop over the first 100 integers.
for (int i = 0; i < 100; i++) {
System.out.println(i);
// Break when the variable equals 5.
if (i == 5) {
break;
}
}
}
}0
1
2
3
4
5
While-true loop. Break is most useful in while-loops. Here we use a while-true loop, which repeats infinitely until broken. If the random number is low, we break the loop.
Note Some researchers have found that breaking from a while-loop is more intuitive for students than using for-loops.
However When we use this style of code, we must be careful that the loop terminates. An infinite loop is a hazard.
public class Program {
public static void main(String[] args) {
// Use a while-true loop.
while (true) {
double number = Math.random();
System.out.println(number);
// Break if random number is less than a certain value.
if (number <= 0.2) {
break;
}
}
}
}0.5650302991616442
0.64936100765301
0.18994610761919195
Switch with break. A switch statement has case blocks. If a break (or return) statement does not terminate a case, control will fall through to the next case.
Note Fall-through occurs even when the value does not match the case labels. Only the first case is matched.
Here We do not use a break in case 0. But case 1 has a break. So when we switch on 0, id is incremented twice.
public class Program {
static int getId(int index) {
int id = 0;
switch (index) {
case 0:
// No break is in this case block, so control falls through.
id++;
case 1:
// Control breaks in this block.
id++;
break;
case 2:
// Set id to 100.
id = 100;
}
return id;
}
public static void main(String[] args) {
int id = getId(0);
System.out.println(id);
id = getId(1);
System.out.println(id);
id = getId(2);
System.out.println(id);
}
}2
1
100
Benchmark, break. Here we test the performance of a break statement in a loop. The two loops (sum1 and sum2) are not precisely equivalent.
Version 1 This version loops through a range and sums all elements in the range. It starts at 0 and ends at max.
Version 2 This loops through the entire array, and breaks when the max is reached. It sums all elements up to this point.
Result The sum1 loop is much faster. It has no break statement, and the loop design is simpler.
So When designing loops, it is best to specify a range, and avoid the break-keyword. Break may cause a slowdown.
public class Program {
static int sum1(int max, int[] array) {
// Loop over range in a for-loop.
int sum = 0;
for (int i = 0; i < max; i++) {
sum += array[i];
}
return sum;
}
static int sum2(int max, int[] array) {
// Loop over entire array, and use break to stop.
int sum = 0;
for (int i = 0; i < array.length; i++) {
if (i >= max) {
break;
}
sum += array[i];
}
return sum;
}
public static void main(String[] args) {
int[] values = { 10, 20, 30, 40, 50, 60, 70, 80 };
long t1 = System.currentTimeMillis();
// Version 1: use for-loop with range.
for (int i = 0; i < 100000000; i++) {
sum1(5, values);
}
long t2 = System.currentTimeMillis();
// Version 2: use for-loop with range and break.
for (int i = 0; i < 100000000; i++) {
sum2(5, values);
}
long t3 = System.currentTimeMillis();
// ... Times.
System.out.println(t2 - t1);
System.out.println(t3 - t2);
}
} 5 ms, sum1: For-loop
254 ms, sum2: For-loop with break
A review. Break is an important keyword in Java. It affects the flow of control. It influences what statements are next reached, in a loop or in a switch.
Other control statements, like continue or return, may take the place of break. These statements create branches in code. They change the next statement executed.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.