The break Statement

Sometimes when executing a loop, you’ll want to leave the loop as soon as a certain condition occurs—for instance, maybe you detect an error condition or find the data you’re looking for in a list of data. You can use the break statement for this purpose. Execution of the break statement causes the program to immediately exit from the loop it is executing, whether it’s a for, while, or do loop. Subsequent statements in the loop are skipped and execution of the loop is terminated. Execution continues with whatever statement follows the loop.

If a break is executed from within a set of nested loops, only the innermost loop in which the break is executed is terminated.

The format of the break statement is simply the keyword ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.