Please enable JavaScript to view this site.

 

Navigation: Macros > Macro Language > Compiler Messages > Errors

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

Scroll Prev Up Next More

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 example:

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, last updated on 18-Apr-2020