Skip to main content
Logo image

Section 7.9 The break statement

The break command needs to be used with utmost caution. Most good programs can and should be written without use of the break statement.
The break statement terminates the execution of a for or while loop. Statements in the loop after the break statement do not execute.
The break statement exits the loop completely. To only skip the rest of the instructions in the loop and begin the next iteration, use a continue statement.
The break statement is not defined outside of a for or while loop. Here is an example:
while (~done)
    if (first)
        first = 0;
        continue;
    elseif (broken)
        break;
    end
    commands
    ...
end
What happens in nested loops?
In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.