Using FOR loop counter variable to set the loop's bounds or step value

Navigation:  Messages > Macro Compiler Messages > Errors >

Using FOR loop counter variable to set the loop's bounds or step value

Previous pageReturn to chapter overviewNext page

This compiler error will occur if, within the for instruction (not within the loop itself), you try to use the counter variable to set either of the start point, end point and step value of the loop.

 

for nCtr = nStart to nEnd step (nCtr + 1) {<- No !!!}

 // some instructions

endfor

 

Using the counter variable (which changes automatically at the end of each loop iteration) in the manner above, is akin to modifying the bounds/step of a for loop, which is also forbidden.

 

The reasons behind this restriction are :

 

Shifting the parameters of a for instruction makes it hard to follow for human readers, including the person writing the Macro, and can easily lead to bugs.

 

The compiled code is optimized to take advantage of the fact that all the elements of a for loop remain constant, during the loop. Changing this would make for slower execution of loops at run-time.

 

Allowing the end point or step value to change could lead to an infinite loop (as would happen in the above example).

 

If you feel you must absolutely change the value of any the for loop variables, this is a sign that you should be using a while loop instead of a for loop. For a discussion on which of the loops to use, see Looping Instructions.


Topic 133700 updated on 01-Nov-2016.
Topic URL: http://www.qppstudio.net/webhelp/index.html?usingthecountervariableofaforl.htm