Loops repeat code numerous times and stop when the specified end point is reached or when the break keyword is executed. But sometimes loops don’t end. What’s going on with those loops?

In this article:

Infinite loops in MultiCharts PowerLanguage

All PowerLanguage loops should contain code that will stop the loop at some point, whether this being a condition (like while someCondition = true in the while loop) or based on a counter variable (such as the for-to loop’s for 0 to 10).

But when the code to stop the loop is missing or not executed, the loop will keep running. Such a loop is common enough to be given its own name: an infinite loop (Liberty & Cadenhead, 2011; Stellman & Greene, 2010) or sometimes called a forever loop (Liberty & MacDonald, 2009).

In programming languages like C# and C++, an infinite loop is sometimes deliberately used when waiting on some event to occur (see Liberty & MacDonald, 2009; Oualline, 2003). In PowerLanguage this isn’t a good idea since infinite loops make the MultiCharts program unresponsive. When waiting on some event, recalculating the script periodically with RecalcLastBarAfter() is a better idea.

The forever running while loop in PowerLanguage

A while loop continues as long as the true/false expression is true (MultiCharts Wiki, 2012a). The code inside the loop should make this expression false at some point. But when that doesn’t happen, an infinite loop is the result:


Variables:
    x(0);

x = 0;

while x < 30 begin

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

end;

x = x + 1;

This while loop continues as long as x is less than 30. But because the value of x is changed outside the loop, the condition x < 30 will remain true forever causing a never-ending loop.

Tip: Infinite loop errors are most likely to happen with the while loop and repeat-until loop. Double-checking the code inside these loops before executing the script helps to spot these errors.

An infinite PowerLanguage while loop due to continue

But some while loop errors are less apparent, like the following:


Variables:
    y(0);

y = 10;

while y > 0 begin 

    Print(y);

    if (y = 3) then 
        continue;

    // With more code here, the error
    // would be less apparent

    y = y - 1;

end;

This while loop continues as long as the y variable is greater than 0, which starts with a value of 10 and is decreased with 1 on each loop cycle.

The problem here is that continue is executed when y is 3. Because continue skips a loop cycle immediately, the part of the loop that changes the y variable is ‘jumped over’ and the loop’s true/false expression remains true forever.

An infinite repeat-until loop in MultiCharts PowerLanguage

The repeat-until loop continues until its true/false expression becomes true. But when this expression never becomes true, an infinite loop is created:


Variables:
    z(0);

z = 0;

repeat

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

until z < 0;

The z variable starts with a value of 0 and is increased with 1 in the loop’s body. This repeat-until loop stops when z < 0 becomes true. But this will not happen, since z has a value of 1 after the first loop cycle and only becomes bigger with each loop cycle.

An infinite PowerLanguage for-to loop

The for-to loop and down-to loop automatically change the counter variable after each loop cycle, so this cannot be accidentally forgotten. But infinite loops can still happen with these loops:


for value1 = 0 to 10 begin 

    Print(value1);

    // With more code here, the error
    // would be less apparent

    value1 = 3;

end;

This for-to loop runs from 0 to 10 with the value of value1 incremented at the end of each loop cycle. But the value1 variable is also set to 3 with each loop cycle, causing the loop to never reach the ending value of 10.

Tip: Generic variable names are better not used as a counter variable. As the example shows, ambiguous variables like value1 can accidentally be used inside the loop for other purposes. A better idea would be to have a few variable names that you only uses as counter or index variables (like x, y, or ii).

To learn more about infinite loops, see troubleshooting infinite loops in PowerLanguage.

Summary

Loops repeat programming code as long as the loop’s statement specifies or until the break keyword is executed. An infinite loop is a loop that runs forever. This error is caused by programming code that prevents the loop from reaching its end point.


References

Liberty, J. & Cadenhead, R. (2011). Sams Teach Yourself C++ in 24 Hours. 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.

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

Oualline, S. (2003). Practical C++ Programming (2nd edition). Sebastopol, CA: O’Reilly.

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

Stellman, A. & Greene, J. (2010). Head First C#: A Brain-Friendly Guide (2nd edition). Sebastopol, CA: O’Reilly Media.