The most common way a loop is stopped is when its true/false expression becomes false. But in certain situations you might want to exit a loop earlier or use multiple exit conditions that can all stop a loop. How to do that?

In this article:

Exit a loop immediately in PowerLanguage with break

The break keyword stops a loop immediately (MultiCharts Wiki, 2012a) and code execution will continue after the end of the loop. Any programming code inside the loop that is between break and the end of the loop will be skipped.

In a for-to loop the basic pattern of break is:


for counter = beginValue to endValue begin 

    // Code to execute repeatedly

    if (trueFalseExpression = true) then
        break;

end;

In a while loop the basic use of break looks as follows:


while trueFalseExpression = true begin 

    // Code to execute repeatedly

    if (anotherTrueFalseExpression = true) then 
        break;

end;

In both of these examples break is executed after an if statement evaluates an expression. Should we execute break without a condition, it is triggered on the first loop cycle and the code is never repeated. That would defeat the purpose of using a loop.

The break keyword is somewhat similar to the #return keyword, but #return skips all remaining programming code (see MultiCharts Wiki, 2012b) whereas break only ‘jumps over’ the remaining code that is inside the loop and then code execution continues below the loop.

Breaking out of a down-to loop

An example of the break keyword in a down-to loop is the following:


Variables:
    x(0);

for x = 10 downto 0 begin 

    if (x = 5) then begin
        Print("Breaking out of the loop");
        break;
    end;

    Print("x = ", x);

end;

Print("Loop ended");

//> x =   10.00
//> x =    9.00
//> x =    8.00
//> x =    7.00
//> x =    6.00
//> Breaking out of the loop
//> Loop ended

This down-to loop starts at 10 and plans to continue down to 0. But when the x variable equals 5, break is executed and the loop is aborted in the middle of a loop cycle. This way the value of x is printed only for the values 10 to 6.

Exiting out of an infinite loop with break

An infinite loop is a loop that runs forever. But we can always use break to stop ‘forever’ loops:


Variables:
    y(0);

y = 0;

while true begin 

    Print("y = ", y);

    y = y + 1;

    if (y > 4) then 
        break;

    if (y = 0) then begin 
        Print("Something went wrong; y is still 0");
        break;
    end;

end;

//> y =    0.00
//> y =    1.00
//> y =    2.00
//> y =    3.00
//> y =    4.00

This while loop is an infinite loop since it continues as long as its true/false expression is true (while (true)), which it always is.

Inside the loop, two if statements can execute the break keyword. The loop stops when y is greater than 4, causing loop to run at most 5 times. And when the value of y is at the end of the loop still 0, we assume something went wrong and exit the infinite loop with break. Such a situation can happen when the y variable is based on a calculation or user input.

For more on break, see breaking out of nested PowerLanguage loops.

Summary

The break keyword immediately stops a loop. Any programming code inside the loop between break and the end of the loop is skipped and code execution continues after the end of the loop.


References

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

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