continue Instruction

The continue instruction causes the next iteration of the innermost matching while or foreach loop to begin.

Syntax

The syntax of the continue instruction is:

continue [label];

Description

In the case of a while loop, the continue instruction passes control to the test of the while condition. In a foreach loop, control passes to the point at which the control variable is assigned its next value.

If you specify a label, control passes to the next iteration of the loop instruction matching the label.

The continue instruction alleviates the need for code within loops becoming too deeply nested. For example, consider a loop that reads some input data, checks that it is valid, and if so, processes it.

If there are a number of validity tests or they are complex, the code may look like that shown in the code fragment in the following example.

while moreData do
    readData;
    if checkFirstDataItem then
        if checkSecondDataItem then
            ...
                ...
                    if checkLastDataItem then
                        processData;
                    endif;
                ...
            ...
        endif;
    endif;
endwhile;

The following example shows the code in the above example rewritten without the deep indentation, by using the continue instruction.

while moreData do
    readData;
    if not checkFirstDataItem then
        continue;
    endif;
    if not checkSecondDataItem then
        continue;
    endif;
    ...
    if not checkLastDataItem then
        continue;
    endif;
    processData;
endwhile;

Example

The following example displays the integers 1 through 10 except for 5 in ascending order, by using the continue instruction.

vars
    count : Integer;
begin
    foreach count in 1 to 10 do
        if count = 5 then
            continue;
        endif;
        write count;          // Outputs 1 2 3 4 6 7 8 9 10
    endforeach;
end;