When programming there are situations in which you’ll want to do the same thing again and again, perhaps each time only in a slightly different way. This process of repeatedly executing the same programming code is called looping or iterating (Liberty & MacDonald, 2009).

In this article:

What are programming loops?

So a loop is a block of programming code that is executed more than once (Liberty & Cadenhead, 2011), and each pass through the loop is what we’ll call a loop cycle. Loops either repeat code a certain number of times or until some condition occurs (Oualline, 2003).

Looping is like an assembly line (Liberty & MacDonald, 2009). On an assembly line, you might take 100 car bodies and mount a wheel on each of them. In a PowerLanguage loop, you might work through 100 price bars and take the volume of each bar in order to calculate the average volume.

Besides going through price bars, loops are also often used with collections like arrays.

The type of loops in PowerLanguage

MultiCharts PowerLanguage has several loop types with the following defining characteristics:

  • The for-to loop, which runs from a begin value to an end value.
  • The down-to loop, on the other hand, runs from a begin value down to an end value.
  • A while loop continues as long as a true/false expression is true.
  • And the repeat-until loop repeats programming code until a true/false expression becomes true.

Each of these loops can be controlled with several PowerLanguage keywords.

Controlling loops with PowerLanguage keywords

The following PowerLanguage keywords influence the behaviour of a loop:

These keywords can also be used with nested PowerLanguage loops.

Placing loops inside another: nested PowerLanguage loops

A loop contained inside another loop is called a nested loop (Liberty & Cadenhead, 2011; Sempf, Sphar, & Davis, 2010). Such loops can do things like going through price bars from multiple data series or going through the elements of a multidimensional array.

Nested PowerLanguage loops are discussed in the following articles:

Nested loops, like regular loops, can become stuck in calculation mode due to an infinite loop.

Infinite loops in PowerLanguage

Loops normally run a certain number of times or until some condition is true. But loops can also run indefinitely, which is called an infinite loop (Liberty & Cadenhead, 2011) or sometimes a forever loop (Liberty & MacDonald, 2009).

That programming error makes the MultiCharts program unresponsive. The following articles take a closer look at infinite loops:

The chapter on looping in PowerLanguage concludes with a summary of looping in PowerLanguage.


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.

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.