Please enable JavaScript to view this site.

 

Navigation: Macros > Macro Language > Compiler Messages > Errors

Modifying the bounds/step of a FOR loop inside the loop is forbidden

Scroll Prev Up Next More

This compiler error will occur if, within a for loop, you try to modify the value of any of the variables that are used to calculate the expressions for the start point, end point and step value of the loop.

 

For example:

for nCtr = nStart to nEnd step nInc
   nStart = nCtr + 2 // NO !!!
   nEnd   = nCtr + nStart // NO !!!
   nInc   = nTokenDayValue div 2 // NO !!!
endfor

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).

 

Note that using the counter variable (which changes automatically at the end of each loop iteration) in any of the expressions used to calculate the start point, end point and step value of the loop, is also forbidden.

 

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.

 

See also: modifying the counter variable within a for loop is forbidden.

 


Topic 108998, last updated on 18-Apr-2020