The for-to loop and down-to loop repeat programming code for a fixed amount of loop cycles. And the while loop continues as long as an expression is true. But all require that the loop’s expression is true at least once before the loop’s code is executed. What if you always wanted to execute your loop for at least one loop cycle?

In this article:

The repeat-until loop in PowerLanguage

The repeat-until loop repeats programming code until a true/false expression is true (Henry MultiCharts, 2014). The repeat-until loop has the following default pattern (MultiCharts Support, personal communication, October 16, 2014; TJ, 2010):


repeat 

    // Code to execute repeatedly
    // and that makes the 'trueFalseExpression'
    // true at some point.

until trueFalseExpression = true;

After the first loop cycle the true/false expression is evaluated. If it’s false, a new loop cycle begins; if it’s true, the loop stops.

The bottom-tested PowerLanguage repeat-until loop

The repeat-until loop is a bottom-evaluated loop, like the do-while loops in C# (see Dorman, 2010) and C++ (see Lischner, 2003). Such loops test the true/false expression after executing the code inside the loop, and so they always run at least once regardless of the true/false expression.

An example of this feature is the following:


Variables:
    z(0);

z = 10;   

repeat
    Print("z = ", z);
    z = z + 1;
until z > 5;

//> z =   10.00

This repeat-until loop runs until z is greater than 5. But when this variable has a value of 10 on the first loop cycle, the code inside the loop is still executed. That’s because the z > 5 expression is tested for the first time after the first loop cycle.

In other words, PowerLanguage doesn’t know that the loop shouldn’t be executed on the first loop cycle because this is tested afterwards.

The repeat-until loop programming errors

As with the while loop, the programmer should make sure that the repeat-until loop exits at some point to prevent an infinite loop. Such an error is less likely with the down-to loop and for-to loop because those automatically change the counter variable after each loop cycle (see MultiCharts Wiki, 2012).

Tip: A repeat-until loop can continue forever if the true/false expression never becomes true. Double-check your programming code, especially when the repeat-until loop has complex logic, before executing the script. For fixing infinite loop errors, see troubleshooting infinite PowerLanguage loops.

The repeat-until loop and the begin and end keywords

The repeat-until loop is the only PowerLanguage loop that doesn’t require the begin and end keywords. But they can be added optionally by including a semicolon (;) between end and until (Henry MultiCharts, 2014) like this:


repeat begin

    // Code to execute repeatedly
    // and that makes the 'trueFalseExpression'
    // true at some point.

end; until trueFalseExpression = true;

Because that semicolon looks out of place to me and potentially confusing, the other repeat-until loop examples don’t use the begin and end keywords.

MultiCharts PowerLanguage’s repeat-until loop example

A basic repeat-until loop is the following:


Variables:
    x(0);

x = 0;

repeat
    Print("x = ", x);
    x = x + 1;
until x > 4;

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

This repeat-until loop outputs the value of x with the Print() keyword, followed by incrementing this variable with 1 with each loop cycle. The loop continues until the value of x is greater than 4.

Summary

The repeat-until loop continues until its true/false expression is true. An infinite loop can be triggered when this expression never becomes true. Because the repeat-until loop is a bottom-tested loop, it’s always executed at least once.


References

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

Henry MultiCharts (2014, October 27). Do you also find PowerLanguage somewhat sloppy? – forum discussion. Retrieved on October 28, 2014, from http://www.multicharts.com/discussion/viewtopic.php?f=1&t=47206&p=108895#p109844

Lischner, R. (2003). C++ in a Nutshell: A Language & Library Reference (Fifth edition). Sebastopol, CA: O’Reilly Media.

MultiCharts Wiki (2012, February 19). For. Retrieved from http://www.multicharts.com/trading-software/index.php/For

TJ (2010, April 24). All about Loops and Nests… (A Primer) – forum discussion. Retrieved on October 17, 2014, from http://www.multicharts.com/discussion/viewtopic.php?t=7351#p32840