The break keyword prematurely stops a loop (MultiCharts Wiki, 2012a) and #return the whole current calculation (MultiCharts Wiki, 2012b). But what if we only want to skip a single loop cycle?

In this article:

Jumping to the next loop cycle with continue

The undocumented continue keyword causes code execution to immediately ‘jump’ to the next loop cycle, similar as in other programming languages (see Dorman, 2010; Lischner, 2003). All statements that are between continue and the end of the loop are ‘jumped over’.

More specific, code execution immediately goes back to the true/false (for the while and repeat-until loops) or the counter variable expression (with the for-to and down-to loops) when continue is executed. Then either a new loop cycle can begin or the loop can stop.

In everyday English, the logic of continue is: “don’t execute any more statements in this loop cycle, but instead begin a new loop cycle starting from the beginning of the loop” (Liberty & MacDonald, 2009). So with continue, a loop continues to the next loop cycle.

The PowerLanguage continue keyword

In a down-to loop the basic pattern of continue is:


for counter = beginValue downto endValue begin 

    // Code to execute repeatedly

    if (trueFalseExpression = true) then
        continue;

end;

And the basic pattern is similar in a while loop:


while trueFalseExpression = true begin 

    // Code to execute repeatedly
    // and makes the trueFalseExpression false at some point

    if (anotherTrueFalseExpression = true) then 
        continue;

end;

In both of these examples the continue keyword is executed after an if statement. Should we execute continue without a condition that acts as a filter, a part of the PowerLanguage loop will be skipped on every loop cycle.

Both the continue keyword and the break keyword can be used to control a loop in PowerLanguage. But break exits a loop completely (MultiCharts Wiki, 2012a), whereas continue only exits the current loop cycle.

Skipping loop cycles with the continue keyword

A basic continue example is the following:


Variables:
    x(0);

for x = 0 to 10 begin 

    if (Mod(x, 2) <> 0) then
        continue;

    Print("x = ", x);

end;

//> x =    0.00
//> x =    2.00
//> x =    4.00
//> x =    6.00
//> x =    8.00
//> x =   10.00

This for-to loop runs from 0 to 10, with the x variable automatically incremented on each loop cycle.

The if statement uses the Mod() keyword, which returns the remainder of division (MultiCharts Wiki, 2012c). We use Mod() here to take an action on every nth loop cycle. So when x divides unevenly into 2, the continue keyword is executed and code execution returns back to the top of the loop.

This way the Print() statement is only executed when the x variable is a multiple of 2.

An infinite loop in PowerLanguage caused by continue

Because continue ‘jumps back’ code execution to the beginning of the loop, all code inside the loop after this keyword is not executed. This can have unintended consequences, such as triggering an infinite loop:


Variables:
    y(0);

y = 0;

while y < 10 begin 

    if (y > 5) then
        continue;

    y = y + 1;

end;

This while loop runs as long as y is less than 10, and this variable begins with a value of 0 and is increased with every loop cycle (y = y + 1). The continue keyword is executed when y is greater than 5.

But since continue causes the y = y + 1 statement to be skipped when y is 6, this variable never changes and the loop keeps running forever (because the loop stops when y is 10). See troubleshooting infinite loops in PowerLanguage for other causes of infinite loops.

Tip: When using the continue keyword in a while or repeat-until loop, place it after the statement that changes the variable used in the loop’s true/false expression.

More examples of continue can be found in the skipping nested loop cycles article.

Summary

The continue keyword immediately ‘jumps back’ to the loop expression, only partially completing the current loop cycle. This prevents the programming code between continue and the end of the loop from executing.


References

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

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. Sebastopol, CA: O’Reilly Media.

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

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

MultiCharts Wiki (2012c, February 13). Mod. Retrieved on October 22, 2014, from http://www.multicharts.com/trading-software/index.php/Mod

MultiCharts Wiki (2012b, February 24). Return. Retrieved on October 21, 2014, from http://www.multicharts.com/trading-software/index.php/Return