This article summarises the previous PowerLanguage looping articles to repeat the most important points. Follow the links to the articles for examples and more information.

In this article:

PowerLanguage loops that run a fixed number of times

The for-to loop runs from a begin value to an end value. Its basic pattern looks as follows:


for counter = beginValue to endValue begin 

    // Code to execute repeatedly

end;

The down-to loop, on the other hand, runs from a begin value down to an end value. This loop type has the following basic pattern:


for counter = beginValue downto endValue begin 

    // Code to execute repeatedly

end;

Both the for-to and down-to loop automatically change the counter variable with 1 at the end of each loop cycle.

PowerLanguage loops that run until a certain condition changes

The while loop repeats programming code as long as its true/false expression is true. It basically looks like this:


while trueFalseExpression = true begin

    // Code that needs to be executed repeatedly
    // and that makes the 'trueFalseExpression'
    // false at some point.

end;

The repeat-until loop, on the other hand, repeats its programming code until the true/false expression is true:


repeat 

    // Code to execute repeatedly
    // and that makes the 'trueFalseExpression'
    // true at some point.

until trueFalseExpression = true;

Both the while and repeat-until loop need to have code inside the loop that changes the true/false expression at some point. An infinite loop error is otherwise the result.

Controlling loops with PowerLanguage keywords

All loops can be controlled with the break and continue keywords. break stops a loop early and looks like this in a for-to loop:


for counter = beginValue to endValue begin 

    // Code to execute repeatedly

    if (trueFalseExpression = true) then
        break;

end;

And continue skips the current loop cycle, like this 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;

The difference between both is that break exits a loop completely, while continue only exits the current loop cycle.

Executing code every nth loop cycle

The Mod() keyword can be used to take an action every nth loop cycle, like this:


for counter = beginValue to endValue begin

    if (Mod(counter, 10) = 0) then
        Print("This happens on every tenth loop cycle.");

end;

Nesting loops in PowerLanguage

Loops can be placed inside each other, which creates nested loops. In its basic form, two nested for-to loops look like this:


for outerLoopCounter = beginValue to endValue begin

    for innerLoopCounter = beginValue to endValue begin

        // Code to repeat for each loop cycle
        // of the inner and outer loop

    end;

    // Code to execute for each outer loop cycle

end;

With two nested loops, the nested loop is called the inner loop and the outside loop the outer loop.

Controlling nested loops with break and continue

The continue keyword can also be applied to nested loops, such as:


for outerLoopCounter = beginValue downto endValue begin 

    for innerLoopCounter = beginValue downto endValue begin

        // Skipping a loop cycle of the inner loop:
        if (someCondition = true) then
            continue;

    end;

    // Skipping a loop cycle of the outer loop:
    if (anotherCondition = true) then 
        continue;

end;

Similarly, break can to stop nested loops:


for outerLoopCounter = beginValue to endValue begin 

    for innerLoopCounter = beginValue to endValue begin

        // Stopping the inner loop:
        if (someCondition = true) then
            break;

    end;

    // Stopping the outer loop:
    if (anotherCondition = true) then 
        break;

end;

Both continue and break only affect the loop in which they are executed. So with multiple loops, they need to be executed once for each loop. An alternative is to place nested loops inside a function, like this basic function:


Variables:
    outerCounter(0),
    innerCounterY(0),
    innerCounterZ(0);

for outerCounter = 0 to 25 begin 

    for innerCounterY = 0 to 25 begin 

        for innerCounterZ = 0 to 50 begin 

            if (someCondition = true) then begin 
                // Process the 'someCondition' here
                // by doing something
                #return;
            end;

        end;

    end;

end;

Infinite loops in PowerLanguage

Loops that run forever are called infinite loops. They happen especially with the repeat-until loop and while loop, such as this faulty loop:


Variables:
    counter(0);

counter = 0;

while counter < 10 begin 

    Print("The value of 'counter' is: ", counter);

end;

counter = counter + 1;

When troubleshooting infinite PowerLanguage loops it helps to know the three causes of infinite loops. First, an exit condition can be missing:


Variables:
    x(0);

x = 0;

repeat

    Print("x = ", x);
    x = x + 1;

until false;

Second, the exit condition might not be reachable:


Variables:
    x(0);

x = 0;    
condition1 = Close > Close[1];

while condition1 begin

    condition2 = Close[x] > Open[x+1];
    x = x + 1;

end;

And third, the loop can (partially) restart so that the loop never ends:


Variables:
    x(0);

for x = 50 downto 0 begin 

    Print("x = ", x);

    if (x < 10) then 
        x = x + 25;

end;

This article concludes the looping in PowerLanguage ‘chapter’. See all MultiCharts PowerLanguage programming articles to learn about other topics.