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.
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.
break
is a way to stop a loop, eve none that has not reached its bounds.break
statement has no arguments. We cannot use labels with it. We cannot use it to break
out of nested loops at once.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 loopBreak 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.
while
-loop is more intuitive for students than using for
-loops.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
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.
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
break
Here we test the performance of a break
statement in a loop. The two loops (sum1 and sum2) are not precisely equivalent.
break
statement, and the loop design is simpler.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
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.