Skip to main content

Break

rascal-0.40.17

Synopsis

End the execution of a while, do or for loop.

Syntax

break;

break Label;

Description

A break statement is only allowed inside the body of a While, Do or For statement and is associated with the innermost loop statement in which it is contained. Its effect is to end the execution of the loop.

In case of nested loops, break can also fail to an outer loop if the loop is labeled with Label and the break as well.

Also see Continue and Fail.

Examples

Here is an example using break to find the first number divisible by 3:

rascal>import IO;
ok
rascal>for(int i <- [1 .. 10]){
>>>>>>> if(i % 3 == 0){
>>>>>>> println("i = <i> is divisible by 3");
>>>>>>> break;
>>>>>>> }
>>>>>>>}
i = 3 is divisible by 3
list[void]: []