go to previous page   go to home page   go to next page

Answer:

Mostly you should not use break.


Bad Break

The break statement is necessary when you use a switch statement. But inside loops, it should be avoided. Again, if you think you need it, your loop probably should be rewritten.

Here is a poorly-written function that determines if its argument n is a prime. The function tests trial divisors from 2 up to the square root of n. As soon as one of them is found to divide n, the loop is exited. If the loop reaches the end without finding a divisor, then n is prime.


QUESTION 12:

Improve the function by removing the break.