#2708 closed Bug (No Bug)
re calculate "to" statment in "for ... to" loop
| Reported by: | TommyDDR | Owned by: | |
|---|---|---|---|
| Milestone: | Component: | AutoIt | |
| Version: | 3.3.8.1 | Severity: | None |
| Keywords: | Cc: |
Description
Should be the behavior of a for loop is the same as in other major programming languages.
Exemple showing that "getMax" is called only once.
For $i = 0 To getMax()
ConsoleWrite("$i = " & $i & @LF)
Next
Func getMax()
Local $rand = Random(5, 10, 1)
ConsoleWrite("getMax() : " & $rand & @LF)
Return $rand
EndFunc
The output condition is currently not recalculated each loop while in C, C + +, java, it is. This could avoid some "dimension range excedeed" for example.
Exemple showing "dimension range excedeed" :
Global $tab = [1, 2, 3, 4, 5]
For $i = 0 To UBound($tab, 1)-1
ConsoleWrite($tab[$i] & @LF)
ReDim $tab[3]
Next
Attachments (0)
Change History (4)
comment:1 by , 12 years ago
comment:2 by , 12 years ago
Replying to TommyDDR:
Should be the behavior of a for loop is the same as in other major programming languages.
[...]
The output condition is currently not recalculated each loop while in C, C + +, java, it is. This could avoid some "dimension range excedeed" for example.
I'm not aware of any programming language with that kind of for-to loop that recalculates the to value. The difference in C, C++, Java etc. pp. is that you need to explicitly calculate the to value before entering the loop because that is only a for a while loop. If that value is constant, the optimizer will probably do that for you.
comment:3 by , 12 years ago
| Resolution: | → No Bug |
|---|---|
| Status: | new → closed |
This is not a bug - it is how the language works. And I too dispute whether "other major programming languages" allow changes to the loop limits.
Please open a "Feature Request" if you wish such a feature to be considered.
M23
comment:4 by , 12 years ago
Allowing such a terrible behavior would uselessly slow down 99.99% of the loops using correct bounds and give a dramatic green light to 0.01% loops which attempt to use incorrect bounds due to overly lazy algorithm implementation. The correct implementation in such case requires a different condition, not a simple upper value in a For loop.

That's how For loops work in AutoIt, the To condition is checked at the start of the loop and the $i variable is checked as you loop through it, but not the To value. You can't change the To condition dynamically.