# The break and continue statements

• Introduction

• There are 2 special statement that work ONLY inside a loop statement (i.e., inside a while or a for statement:

• break
• continue

• The break causes the loop statement to terminate immediately

• The continue causes the loop statement to start the next iteration immediately

• The break statement

• When the program is inside a loop statement, and the body of the loop statement contains a break   statement, then the execution of the break   statement, will cause the loop statement to terminate immediately:

• The break   statement is commonly used in programs where a single occurence of a condition cause the loop to complete its task.

• Example:

• Given an integer n
• Determine if n is prime.

We have seen the Java program to determine if a number n is prime:

 boolean isPrime; int f; isPrime = true; // Assume that n is prime... for ( f = 2; f <= n - 1 ; f++ ) { if (n % f == 0) then isPrime = false; // Now we know n is NOT prime... }

Look closely to the loop:

 for ( f = 2; f <= n - 1 ; f++ ) { if (n % f == 0) then isPrime = false; // Now we know n is NOT prime... }

This loop runs the variable f from 2 to n-1 and test if every number is a divider of n

However... if we finds that ONE of these numbers does divide n, we can STOP THE LOOP IMMEDIATELY - because we already know that the number is not prime...

The break statement can be used to break out of the loop in this case !

• Improved Examples:

 boolean isPrime; int f; isPrime = true; // Assume that n is prime... for ( f = 2; f <= n - 1 ; f++ ) { if (n % f == 0) then { isPrime = false; // Now we know n is NOT prime... break; // Exit the loop NOW } }

• Example Program: (Demo above code)

• The continue statement

• When the program is inside a loop statement, and the body of the loop statement contains a continue   statement, then the execution of the continue   statement, will cause the loop statement to start a new iteration immediately:

IMPORTANT

• The execution of the continue   statement differs a little when it is executed inside the while and for statements:

1. When the continue   statement is executed inside a while   statement, the while   statement, will test the Condition   clause immediately.

If the Condition   clause is true , then the while body is executed.
Otherwise, (Condition   clause is false ), then the while   statement is terminated.

2. When the continue   statement is executed inside a for   statement, the for   statement, will:

• Execute the Update   statement
• Then test the Condition   clause

If the Condition   clause is true , then the for body is executed.
Otherwise, (Condition   clause is false ), then the for   statement is terminated.

• The continue   statement is commonly used in programs to skip over unqualified cases and continue with the next case.

• Example:

• Given an integer N
• Find all prime numbers that are less than or equal to N

We have seen such a program before, now we will write one using the continue   statement

 boolean isPrime; int f, n, N; System.out.println(2); // Print 2 - it's the only even prime for ( n = 3; n <= N ; n++ ) { if ( n % 2 == 0 ) continue; // Skip the rest of the for-body // From this point on, the statements are only // executed for ODD values !!! isPrime = true; // Assume that n is prime... for ( f = 3; f <= n - 1 ; f++ ) { if (n % f == 0) then isPrime = false; // Now we know n is NOT prime... } if ( isPrime ) System.out.println( n + " is a prime number"); }

• Concluding Remarks

• You DO NOT need to use these statements to write programs.

Any program that uses break or continue statements can be re-written without using them.

• In fact, some well-known computer scientists (such as N. Wirth and E. Dijkstra) consider the use of these statements in programs to be tacky and dangerous

• In practice, the continue   statement is rarely used.

• However, the use of the break   statement is quite common among Java and C++ programmers...