The break and continue keywords provide additional controls in a loop. Two keywords, break and continue, can be used in loop statements to provide additional controls. Using break and continue can simplify programming in some cases. Overusing or improperly using them, however, can make programs difficult to read and debug. You have used the keyword break in a switch statement. You can also use break in a loop to immediately terminate the loop. The program demonstrate the effect of using break in a loop:
The program adds integers from 1 to 20 in this order to sum until sum is greater than or equal to 100. Without the if statement (line 12), the program calculates the sum of the numbers from 1 to 20. But with the if statement, the loop terminates when sum becomes greater than or equal to 100. Without the if statement, the output would be:
The number is 20
The sum is 210
You can also use the continue keyword in a loop. When it is encountered, it ends the current iteration and program control goes to the end of the loop body. In other words, continue breaks out of an iteration while the break keyword breaks out of a loop. The program demonstrate the effect of using continue in a loop:
The program adds integers from 1 to 20 except 10 and 11 to sum. With the if statement in the program (line 11), the continue statement is executed when number becomes 10 or 11. The continue statement ends the current iteration so that the rest of the statement in the loop body is not executed; therefore, number is not added to sum when it is 10 or 11. Without the if statement in the program, the output would be as follows:
The sum is 210
In this case, all of the numbers are added to sum, even when number is 10 or 11. Therefore, the result is 210, which is 21 more than it was with the if statement.
The continue statement is always inside a loop. In the while and do-while loops, the loop-continuation-condition is evaluated immediately after the continue statement. In the for loop, the action-after-each-iteration is performed, then the loop-continuation-condition is evaluated, immediately after the continue statement.
You can always write a program without using break or continue in a loop. In general, though, using break and continue is appropriate if it simplifies coding and makes programs easier to read.
Suppose you need to write a program to find the smallest factor other than 1 for an integer n (assume n >= 2). You can write a simple and intuitive code using the break statement as follows:
int factor = 2;
while (factor <= n) {
if (n % factor == 0)
break;
factor++;
}
System.out.println("The smallest factor other than 1 for "
+ n + " is " + factor);
You may rewrite the code without using break as follows:
boolean found = false;
int factor = 2;
while (factor <= n && !found) {
if (n % factor == 0)
found = true;
else
factor++;
}
System.out.println("The smallest factor other than 1 for "
+ n + " is " + factor);
Obviously, the break statement makes this program simpler and easier to read in this case. However, you should use break and continue with caution. Too many break and continue statements will produce a loop with many exit points and make the program difficult to read.
Some programming languages have a goto statement. The goto statement indiscriminately transfers control to any statement in the program and executes it. This makes your program vulnerable to errors. The break and continue statements in Java are different from goto statements. They operate only in a loop or a switch statement. The break statement breaks out of the loop, and the continue statement breaks out of the current iteration in the loop.
Programming is a creative endeavor. There are many different ways to write code. In fact, you can find a smallest factor using a rather simple code as follows:
int factor = 2;
while (factor <= n && n % factor != 0)
factor++;
Top comments (0)