Modify

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#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 Changed 6 years ago by BrewManNH

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.

comment:2 in reply to: ↑ description Changed 6 years ago by anonymous

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 Changed 6 years ago by Melba23

  • Resolution set to No Bug
  • Status changed from new to 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 Changed 6 years ago by jchd18

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.

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.