Previously we saw that break immediately stops a loop. But how does break work with nested loops in PowerLanguage?

In this article:

Breaking out of nested PowerLanguage loops

The break keyword immediately stops a loop (MultiCharts Wiki, 2012a). Just like other programming languages, all statements between break and the end of the loop will be ‘jumped over’ and code execution continues after the end of the loop (Sempf, Sphar, & Davis, 2010).

The basic pattern of break in a nested for-to loop is:


for outerLoopCounter = beginValue to endValue begin 

    for innerLoopCounter = beginValue to endValue begin

        if (someCondition = true) then
            break;

    end;

    if (anotherCondition = true) then 
        break;

end;

The break keyword exits the nearest loop, meaning that with nested loops only the innermost loop is exited (Dorman, 2010; Lischner, 2003). So when someCondition is true, the inner loop is stopped early due to break. The script then continues with the outer loop, which is ended early when anotherCondition is true.

We could exit multiple loops at once with #return, but all programming code in the script that comes after #return is then also not executed (see Liberty & Cadenhead, 2011; MultiCharts Wiki, 2012b). That includes programming code that comes after the loops. To exit multiple loops at once, nested loops in a function are a better idea.

Using break with nested PowerLanguage loops

Compared to the continue keyword in a nested loop, break exits the loop completely while continue only exits the current loop cycle. The following example shows this terminating behaviour of break:


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

for outer = 0 to 10 begin 

    if (outer = 5) then 
        break;

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

    for inner = 0 to 10 begin

        if (inner > 2) then
            break;

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

    end;

end;

//> Outer loop = 0
//>    Inner loop = 0
//>    Inner loop = 1
//>    Inner loop = 2
//> Outer loop = 1
//>    Inner loop = 0
//>    Inner loop = 1
//>    Inner loop = 2
//> Outer loop = 2
//>    Inner loop = 0
//>    Inner loop = 1
//>    Inner loop = 2
//> Outer loop = 3
//>    Inner loop = 0
//>    Inner loop = 1
//>    Inner loop = 2
//> Outer loop = 4
//>    Inner loop = 0
//>    Inner loop = 1
//>    Inner loop = 2

Both the inner and outer for-to loop use break here. The inner loop is stopped early when the inner counter variable is greater than 2. In the outer loop, break is executed when the outer variable is 5.

The output shows that the inner loop is executed 3 times for each outer loop cycle, and the outer is executed loop 5 times. When break is executed in the outer loop, the inner loop is also not executed anymore.

Exiting both the inner and the outer loop with break

Since break only applies to the loop in which it’s executed, we can use a true/false expression to exit both the inner and outer loop:


Variables:
    x(0),
    y(0),
    closedHigher(false),
    exitNow(false);

exitNow = false;

for x = 0 to 25 begin 

    for y = 1 to 2 begin 

        closedHigher = (Close[x] Data(y) > Close[x + 1] Data(y)) and 
            (Close[x + 1] Data(y) > Close[x + 2] Data(y));

        if (closedHigher = true) then begin
            Print("Data ", NumToStr(y, 0),
                " closed higher two consecutive times (bar #",
                NumToStr(CurrentBar + MaxBarsBack - x, 0), ")");
            exitNow = true;
            break;
        end;

    end;

    if (exitNow = true) then 
        break;

end;

//> Data 2 closed higher two consecutive times (bar #106)

The outer loop runs from 0 to 25 and is used to loop over the historical price bars. The inner loop, which runs from 1 to 2, loops over the data series.

The closedHigher true/false variable inside the inner loop is true when the current close of a data series is higher than the previous close and if this previous close was higher than the bar that preceded it. When this isn’t the case, closedHigher is false.

When closedHigher is true, Print() outputs information and the exitNow true/false variable is set to true. Then break is executed and the inner loop is stopped prematurely.

In the outer loop, break is executed when this exitNow variable is true. This way, both loops are stopped as soon as the if statement with the closedHigher variable is executed in the inner loop.

Exiting out of nested loops without a true/false expression

Alternatively, we can exit both the inner and outer loop without an extra true/false variable:


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

for x = 0 to 25 begin 

    for y = 1 to 2 begin 

        closedHigher = (Close[x] Data(y) > Close[x + 1] Data(y)) and 
            (Close[x + 1] Data(y) > Close[x + 2] Data(y));

        if (closedHigher = true) then begin
            Print("Data ", NumToStr(y, 0),
                " closed higher two consecutive times (bar #",
                NumToStr(CurrentBar + MaxBarsBack - x, 0), ")");
                    x = 25;
            break;
        end;

    end;   

end;

//> Data 2 closed higher two consecutive times (bar #106)

This example is similar to the previous sample, but with one difference. When closedHigher is true, the x counter variable is given a value of 25 and then break is used to stop the inner loop.

Setting x to 25 (or higher) makes the outer loop statement (for x = 0 to 25 begin) invalid. Because the for-to loop automatically increases the counter variable after each loop cycle, it will have a value of 26 on the next loop cycle after we’ve executed break to stop the inner loop. Since 26 makes the outer loop statement invalid, this loop is also stopped.

Summary

break immediately ends the loop in which it’s executed, causing the programming code between break and the end of the loop to be skipped. Since break only affects the loop in which it’s executed, multiple break keywords are needed with nested loops. Alternatively, the outer loop can be stopped by invalidating its loop statement.


References

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

Liberty, J. & Cadenhead, R. (2011). Sams Teach Yourself C++ in 24 Hours. 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 (2012a, February 24). Break. Retrieved on November 4, 2014, from http://www.multicharts.com/trading-software/index.php/Break

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

Sempf, B., Sphar, C., & Davis, S.R. (2010). C# 2010 All-In-One for Dummies. Hoboken, NJ: John Wiley & Sons.