Previously we discussed skipping loop cycles with continue, and learned that this keyword makes an early start on the next loop cycle. But how does continue work with nested loops?

In this article:

Skipping to the next loop cycle in nested PowerLanguage loops

The PowerLanguage continue keywords works as in other programming languages: it causes code execution to ‘jump back’ to the looping statement for re-evaluating the expression (Lischner, 2003). And statements that are between continue and the end of the loop are skipped (Dorman, 2010). This makes continue behave as a shortcut to the next loop cycle.

The basic pattern of continue with two down-to loops is the following:


for outerLoopCounter = beginValue downto endValue begin 

    for innerLoopCounter = beginValue downto endValue begin

        if (someCondition = true) then
            continue;

    end;

    if (anotherCondition = true) then 
        continue;

end;

The continue keyword only applies to the innermost loop (see Dorman, 2010). So when someCondition is true, the inner loop’s continue keyword is executed and only this loop will begin a new loop cycle. And when anotherCondition is true, continue only affects the loop cycle of the outer loop.

Both continue and the break keyword can be used to control a PowerLanguage loop. But break exits a loop completely (MultiCharts Wiki, 2012), while continue only exits the current loop cycle. For more, see using break with nested loops in PowerLanguage.

Using continue in a nested PowerLanguage loop

The following example shows how continue works with nested loops:


Variables:
    inner(0),
    outer(0);

for outer = 0 to 10 begin 

    if (outer > 3) then 
        continue;

    Print("Outer loop = ", NumToStr(outer, 0));

    for inner = 0 to 10 begin

        if (Mod(inner, 3) <> 0) then
            continue;

        Print(Spaces(5), "Inner loop = ", 
            NumToStr(inner, 0));

    end;

end;   

//> Outer loop = 0
//>     Inner loop = 0
//>     Inner loop = 3
//>     Inner loop = 6
//>     Inner loop = 9
//> Outer loop = 1
//>     Inner loop = 0
//>     Inner loop = 3
//>     Inner loop = 6
//>     Inner loop = 9
//> Outer loop = 2
//>     Inner loop = 0
//>     Inner loop = 3
//>     Inner loop = 6
//>     Inner loop = 9
//> Outer loop = 3
//>     Inner loop = 0
//>     Inner loop = 3
//>     Inner loop = 6
//>     Inner loop = 9

The continue keyword occurs here in the inner and outer loop. In the inner loop, continue is executed when the inner counter variable is not evenly divisible into 3, which is checked with the modulus operator (Mod()). Since this causes the remainder of the loop cycle to be skipped, the variable is only outputted when it’s an even multiple of 3.

In the outer loop, continue is executed when the outer counter variable is greater than 3. When that happens, both the Print() statement and the inner loop below continue will not be executed anymore.

Continuing both the inner and outer loop in PowerLanguage

Because continue only applies to the loop in which it’s executed, a true/false expression is needed when both the inner and outer loop should skip a loop cycle:


Variables:
    x(0),
    y(0),
    continueOuter(false);

continueOuter = false;

for x = 0 to 10 begin 

    for y = 0 to 2 begin 

        if (x > 2) then begin
            continueOuter = true;
            continue;
        end;

        Print("y = ", NumToStr(y, 0));

    end;

    if (continueOuter = true) then 
        continue;

    Print(Spaces(3), "x = ", NumToStr(x, 0));

end;

//> y = 0
//> y = 1
//> y = 2
//>    x = 0
//> y = 0
//> y = 1
//> y = 2
//>    x = 1
//> y = 0
//> y = 1
//> y = 2
//>    x = 2

When the x variable is greater than 2, the continueOuter variable is set to true inside the inner loop. Then continue is executed, but this will only skip loop cycles of the inner loop. So each loop cycle of the outer loop uses an if statement to see whether continueOuter is true. When that is the case, continue is also executed in the outer loop.

Summary

The continue keyword directs code execution back to the start of the looping statement. This causes all statements between continue and the end of the loop to be skipped. This keyword only affects the loop in which it’s executed.


References

Dorman, S. (2010). Sams Teach Yourself Visual C# 2010 in 24 Hours. Indianapolis, IN (USA): Sams/Pearson Education.

Lischner, R. (2003). C++ in a Nutshell: A Language & Library Reference (Fifth edition). Sebastopol, CA: O’Reilly Media.

MultiCharts Wiki (2012, February 24). Break. Retrieved on November 1, 2014, from http://www.multicharts.com/trading-software/index.php/Break