Further controlling loops with break and continue
Rather than relying on goto
to get out of sticky situations inside of deeply nested statements, the creators of C provided two very controlled goto
-like mechanisms. These are break
and continue
.
break
jumps out of and to the end of the enclosing statement block, whereas continue
is used for looping, which goes immediately to the next iteration of the looping statement, skipping any statements that would otherwise be executed in the loop after the continue
mechanism.
We have previously encountered the use of break
in the switch
statement in the preceding chapter, where break
caused the execution to resume immediately after the switch
statement block. break
can also be used to jump to the end of the enclosing statement_body
loop.
In the following isPrime()
function, break
is used to get out of a loop that determines whether the given number is divisible by the counter value; if so, the number is not prime.
The isPrime()
function...