break Instruction

The break instruction breaks out of a loop.

Syntax

The syntax of the break instruction is:

break [label];

Description

More precisely, a break instruction passes control to the first instruction following the innermost matching loop end.

If you specify a label, then control passes to the first instruction following the end matching the label.

Examples

The code fragment in the following example shows the use of the break instruction to conditionally terminate a foreach iteration.

foreach count in 1 to self.length do
    if self[count] <> " " then
        strOut := self[count : self.length - count + 1];
        break;
    endif;
endforeach;

The following code fragment is another example of the use of the break instruction to conditionally terminate a foreach iteration.

foreach task in tasklist do
    if task.scheduled then
        break;
    endif;
    task := null;
endforeach;
if task <> null then
    runScheduledTask (task);
endif;

The following code fragment is an example of the use of the break instruction with a label to terminate a while loop from a nested foreach instruction.

found := false;
while count < Max_Tables do : outerloop
    wordtable := tables[count];
    foreach word in wordtable do
        if token = word then
            found := true;
            break outerloop;
        endif;
    endforeach;
    count := count + 1;
endwhile outerloop;

Execution of the break instruction in the above example causes termination of the outer while loop and execution continues with the next instruction following the endwhile.

The outerloop label identifier that follows the endwhile is optional but if it is present, the Jade compiler checks that it matches the label following the matching do.