Break Statements

The break statement can alter the flow of a normal loop. Loops iterate over a block of code until the Boolean expression is false, but sometimes we wish to terminate the iteration or even the whole loop early. The break statement is used in these cases.

The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop. If the break statement is inside a nested loop (loop inside another loop), the break will terminate the innermost loop only. Note you will most likely need to place an if statement inside the loop to use the break statement, because if you just have a break statement all by itself inside a loop, it will always hit it the first time through and that is not really useful!

The break statement (in most computer programming languages) takes the generic form of:

WHILE bolean expression
statement_1
statement_2
IF bolean expression THEN
BREAK
ENDIF
counter = counter + 1
END
FOR counter in range(n)
statement_1
statement_2
IF bolean expression THEN
BREAK
ENDIF
END

The flowchart for a Break statement will look like this:

Break Statement

The following code snippet is a break program: