The break keyword can exit nested loops. But this keyword only applies to the innermost loops, which requires multiple break keywords to exit multiple nested loops. That makes the code more complex and error-prone. Is there an easier way?

In this article:

Stopping code execution in PowerLanguage: break or #return

Loops can be stopped in multiple ways. With break a loop immediately stops (MultiCharts Wiki, 2012a) and code execution continues after the loop.

Loops can also be stopped with #return, but this is a far-reaching keyword: all programming code after it will not be executed (see Liberty & Cadenhead, 2011; MultiCharts Wiki, 2012b). So the statements below the loop that executes #return are also not processed.

Nested loops inside a MultiCharts PowerLanguage function

But when programming code is placed inside a function, #return can be used to exit the function immediately. Doing so will exit all loops, no matter how many nested loops there are (see Sempf, Sphar, & Davis, 2010). And it makes the code easier to understand because multiple break keywords aren’t needed.

To see how this works, we’ll first look at nested loops, then add break keywords, and finally move the code to a function.

Multiple nested loops with PowerLanguage

The following example uses multiple nested for-to loops:


Variables:
    x(0),
    y(0),
    z(0),
    theSum(0);

theSum = 0;

for x = 0 to 10 begin 

    for y = 0 to 10 begin 

        for z = 0 to 10 begin 

            theSum = theSum + x + y + z;

            Print("x, y, z = ", 
                NumToStr(x, 0), Spaces(2),
                NumToStr(y, 0), Spaces(2),
                NumToStr(z, 0), Spaces(2),
                "Running total: ", NumToStr(theSum, 0));             

        end;

    end;

end;

//> x, y, z = 0  0  0  Running total: 0
//> x, y, z = 0  0  1  Running total: 1
//> x, y, z = 0  0  2  Running total: 3
//> x, y, z = 0  0  3  Running total: 6
//> x, y, z = 0  0  4  Running total: 10
//> x, y, z = 0  0  5  Running total: 15
//> x, y, z = 0  0  6  Running total: 21
//> x, y, z = 0  0  7  Running total: 28
//> x, y, z = 0  0  8  Running total: 36
//> etc..

Each of these loops run 11 times (from 0 to 10). A variable inside the innermost loop (theSum) holds the running total of all loop counter variables. Let’s say we want to exit all loops when theSum is greater than 15. How to do that with break?

Exiting multiple nested loops in MultiCharts PowerLanguage

Since break only exits the loop in which it’s executed, several break keywords are needed to exit out of nested loops:


Variables:
    x(0),
    y(0),
    z(0),
    theSum(0);

theSum = 0;

for x = 0 to 10 begin 

    for y = 0 to 10 begin 

        for z = 0 to 10 begin 

            theSum = theSum + x + y + z;

            if (theSum > 15) then begin
                Print("Stopping the for z = 0 to 10 loop..");
                break;
            end;

            Print("x, y, z = ", 
                NumToStr(x, 0), Spaces(2),
                NumToStr(y, 0), Spaces(2),
                NumToStr(z, 0), Spaces(2),
                "Running total: ", NumToStr(theSum, 0));             

        end;

        if (theSum > 15) then begin
            Print("Stopping the for y = 0 to 10 loop..");
            break;
        end;

    end;

    if (theSum > 15) then begin
        Print("Stopping the for x = 0 to 10 loop..");
        break;
    end;

end;

//> x, y, z = 0  0  0  Running total: 0
//> x, y, z = 0  0  1  Running total: 1
//> x, y, z = 0  0  2  Running total: 3
//> x, y, z = 0  0  3  Running total: 6
//> x, y, z = 0  0  4  Running total: 10
//> x, y, z = 0  0  5  Running total: 15
//> Stopping the for z = 0 to 10 loop..
//> Stopping the for y = 0 to 10 loop..
//> Stopping the for x = 0 to 10 loop..

In each loop the break keyword is executed when theSum is greater than 15: first inside the innermost loop, then in the first nested loop, and finally in the outer loop. While these are simple loops, with increasing complexity the odds increase that a break is forgotten or an if statement is mistyped.

Tip: place complex loops inside a function

Exiting multiple loops at once can be simplified with placing the loops inside a function (numeric return type) and using #return:


Variables:
    x(0),
    y(0),
    z(0),
    theSum(0);

theSum = 0;

for x = 0 to 10 begin 

    for y = 0 to 10 begin 

        for z = 0 to 10 begin 

            theSum = theSum + x + y + z;

            if (theSum > 15) then begin 
                NestedLoopFunction = theSum;
                Print("Exiting the function..");
                #return;
            end;

            Print("x, y, z = ", 
                NumToStr(x, 0), Spaces(2),
                NumToStr(y, 0), Spaces(2),
                NumToStr(z, 0), Spaces(2),
                "Running total: ", NumToStr(theSum, 0));

        end;

    end;

end;

This way multiple break keywords aren’t needed. We just optionally assign a value to the function (NestedLoopFunction = theSum) and then execute #return to exit the function.

The indicator or trading strategy can execute this function as follows:


Variables:
    theSum(0);

theSum = NestedLoopFunction;

Print("The sum = ", NumToStr(theSum, 0));

//> x, y, z = 0  0  0  Running total: 0
//> x, y, z = 0  0  1  Running total: 1
//> x, y, z = 0  0  2  Running total: 3
//> x, y, z = 0  0  3  Running total: 6
//> x, y, z = 0  0  4  Running total: 10
//> x, y, z = 0  0  5  Running total: 15
//> Exiting the function..
//> The sum = 21

This shows that moving nested loops to a function not only makes the loop code cleaner but also makes the main script more manageable.

Summary

break only exits the loop in which it’s executed, and so in nested loops this keyword needs to be executed repeatedly. An efficient way to exit multiple nested loops at once is moving the code to a function and using #return inside the function.


References

Liberty, J. & Cadenhead, R. (2011). Sams Teach Yourself C++ in 24 Hours. IN (USA): Sams/Pearson Education.

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

MultiCharts Wiki (2012b, February 24). Return. Retrieved on November 8, 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.