Overview
At times, it is desirable to exit from a loop or short-circuit the current iteration of a loop. The break and continue statements make it happen.
The break statement
Can only appear within a loop or a switch statement.
Causes an immediate exit from a loop structure before the test condition is met. Once a break statement is encountered, the loop immediately terminates, skipping any remaining code. For example,
for (int x = 0; x < 10; x++) {
if (x == 5)
break;
else
System.out.print(" " + x);
}will end prematurely when x takes on a value of 5. The output displayed will be:
0 1 2 3 4
May specify the label of the loop to be abandoned. This makes it possible to abandon a specific loop. For example,
outer: for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= 1; j--) {
if (i == j)
break outer;
else
System.out.println("i = " + i + ", j = " + j);
}
}
will end the outer loop when i equals j. The output displayed will be:
i = 1, j = 5
i = 1, j = 4
i = 1, j = 3
i = 1, j = 2
Causes an immediate exit from a switch, skipping any remaining code. For example, if key is a char variable having a value entered by the user, the following statements will display whether they entered an 'A' or a 'B':
switch (key) {
case 'a':
case 'A':
System.out.println("You entered an 'A'");
break;
case 'b':
case 'B':
System.out.println("You entered a 'B'");
break;
default:
System.out.println("You did not enter an 'A' or a 'B'");
break;
}
The continue statement
Can only appear within a loop.
Skips all remaining statements within the loop and resumes with the next iteration. For example,
for (int x = 0; x < 10; x++) {
if (x == 5)
continue;
else
System.out.print(" " + x);
}
will short-circuit when x takes on a value of 5. The output displayed will be:
0 1 2 3 4 6 7 8 9
May specify the label of the loop to be continued. This makes it possible to skip to the next iteration of the specified loop. For example,
outer: for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= 1; j--) {
if (i == j)
continue outer;
else
System.out.println("i = " + i + ", j = " + j);
}
}
will short-circuit to the next iteration of the loop labeled outer when i equals j. The output displayed will be:
i = 1, j = 5
i = 1, j = 4
i = 1, j = 3
i = 1, j = 2
i = 2, j = 5
i = 2, j = 4
i = 2, j = 3
i = 3, j = 5
i = 3, j = 4
i = 4, j = 5
Review questions
Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?
int x = 7;
if (x > 6) {
break;
System.out.println("Big number");
}
else {
System.out.println("Small number");
}
the statements will not compile
the statements will compile but a run time error will occur
the statements will compile but nothing will be displayed
the statements will compile and "Big number" will be displayed
the statements will compile and "Small number" will be displayed
Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?
while (true) {
for (int i = 3; i > 0; i--) {
if (i == 3)
continue;
else
System.out.println(i);
}
break;
}
System.out.println("All done");
the statements will not compile
the statements will compile but a run time error will occur
2
1
2
1
(in an endless loop)2
1
All doneAll done
Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?
for (int i = 1; i <= 3; i++) {
for (int j = 3; j >= 1; j--) {
if (i != j)
continue;
else
System.out.println("i = " + i + ", j = " + j);
}
}
the statements will not compile
the statements will compile but a run time error will occur
i = 1, j = 1
i = 2, j = 2
i = 3, j = 3i = 3, j = 3
the statements will compile but nothing will display
Assuming all unseen code is correct, what will happen when an attempt is made to compile and execute the following statements?
for (int i = 1; i <= 3; i++) {
for (int j = 3; j >= 1; j--) {
if (i != j)
break;
else
System.out.println("i = " + i + ", j = " + j);
}
}
the statements will not compile
the statements will compile but a run time error will occur
i = 1, j = 1
i = 2, j = 2
i = 3, j = 3i = 3, j = 3
the statements will compile but nothing will display