While constructs provide a means to repeatedly execute one or more portions of the macro based on a condition that changes during the execution. A while construct consists of the following components:
The while expression must include a lone environment value command, a local variable, a literal, or some operation using one or more operators. Each time that this expression evaluates to nonzero, the associated expression group is executed.
You can place an iteration expression after the while expression. This optional expression is evaluated after each execution of the while expression group.
You can include if structures within a while structure. You can also include special control expressions indicated by the break or continue expressions. The break expression breaks out of the while structure by halting execution of the expression group and executing the first expression after the endwhile statement. The continue expression skips over the rest of the expression group, evaluates any iteration expression, then continues with the execution of the while structure. The while structure is limited to 100,000 repetitions by default. You can nest up to 10 while structures.
Example
The following sample macro demonstrates various while structures:
<# while_examples #> <# //---------------------------------------- #> <# // Remember that variables are automatically initialized to 0. #> ! Table of squares of the first 10 integers: <# while ++i <= 10 #> !<#i;" ";i*i;"\n"#> <# endwhile #> <# // Remember that the value of a string used as an integer is the number. #> <# // of characters in the string. #> <# stars := "*" #> <# while stars < 10, stars := stars $ "*"#> !<# stars;"\n" #> <# endwhile #> <# while stars > 0, stars := substr(stars, 0, stars-1)#> !<# stars;"\n" #> <# endwhile #> <# // An example of the continue and break statements. #> <# // Also note that many statements can be grouped. #> ! All the positive even numbers less than 11 <# i:=0; while ++i < 100 #> <#if i%2; continue; endif; if i > 10; break; endif; "!" $ i $ "\n"; #> <# endwhile #> <# // While constructs will NOT iterate forever. #> <# while 100 > 0 // This is always true, but the macro will eventually stop #> <# ++iterations; endwhile #> ! The while loop iterated <#iterations#> times. <# endtmpl #>