C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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).
ReturnJava program that uses break in for-loop
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;
}
}
}
}
Output
0
1
2
3
4
5
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.
Java program that breaks from while-true loop
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;
}
}
}
}
Output
0.5650302991616442
0.64936100765301
0.18994610761919195
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.
Java program that uses break in switch
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);
}
}
Output
2
1
100
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.
Java program that benchmarks break-statement
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);
}
}
Output
5 ms, sum1: For-loop
254 ms, sum2: For-loop with break