traditional for loop: for i = a to b (step c) possibilities
Posted: 19 Jan 2018, 03:45
- I have been thinking about traditional for loops, and the best way to achieve them, since I first started using AutoHotkey. It was the one killer feature that Excel macros had, that AutoHotkey did not.
- While thinking about what the very best function/object solution might be (see the links lower down), I considered that using a while loop might be the least-worst solution, of all the solutions available.
- I always felt that Loop was doable/workable, but not quite good enough. The while loop gives a result that I find reasonably satisfactory.
- I find traditional for loops particularly useful for use with offsets with NumGet/NumPut/SubStr, plotting graphs (the values for the axes), and mathematics generally.
- I'm posting here in case anyone else has anything to say re. this.
Links:
[traditional for loop via functions][for i = a to b (step c)]
Traditional For loop (i.e., step through a sequence) - Suggestions - AutoHotkey Community
https://autohotkey.com/board/topic/7122 ... -sequence/
[traditional for loop via objects][for i = a to b (step c)]
For loop question - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/6691 ... ntry423515
enum type and while loop - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/7886 ... hile-loop/
- While thinking about what the very best function/object solution might be (see the links lower down), I considered that using a while loop might be the least-worst solution, of all the solutions available.
- I always felt that Loop was doable/workable, but not quite good enough. The while loop gives a result that I find reasonably satisfactory.
- I find traditional for loops particularly useful for use with offsets with NumGet/NumPut/SubStr, plotting graphs (the values for the axes), and mathematics generally.
- I'm posting here in case anyone else has anything to say re. this.
Code: Select all
q:: ;traditional for loop possibilities
;-20, -16, ... 16, 20
while (vIndex := A_Index*4-4 -20) <= 20
MsgBox, % vIndex
MsgBox
;the while loop looks much better than this Loop equivalent
Loop
{
if !((vIndex := A_Index*4-4 -20) <= 20)
break
MsgBox, % vIndex
}
MsgBox
;although this Loop equivalent isn't too bad,
;it doesn't give you all of the information neatly at the top
Loop
{
vIndex := A_Index*4-4 -20
if !(vIndex <= 20)
break
MsgBox, % vIndex
}
MsgBox
;here's another possibility,
;although again, it doesn't give you all of the information neatly at the top,
;also we've had to change the inequality,
;i.e. 'has it gone beyond the end' to 'will it go beyond the end next time',
;and we have to use the step value in the inequality, which is a further disadvantage
Loop
{
vIndex := A_Index*4-4 -20
MsgBox, % vIndex
} until (vIndex+4 > 20)
MsgBox
MsgBox, % "and now in reverse"
;do the for loop in reverse (a minus sign was added)
;--20, --16, ... -16, -20
;i.e. 20, 16, ... -16, -20
while (vIndex := A_Index*4-4 -20) <= 20
MsgBox, % -vIndex
MsgBox
;do the for loop in reverse (various changes made)
;20, 16, ... -16, -20
while (vIndex := -A_Index*4+4 +20) >= -20
MsgBox, % vIndex
return
[traditional for loop via functions][for i = a to b (step c)]
Traditional For loop (i.e., step through a sequence) - Suggestions - AutoHotkey Community
https://autohotkey.com/board/topic/7122 ... -sequence/
[traditional for loop via objects][for i = a to b (step c)]
For loop question - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/6691 ... ntry423515
enum type and while loop - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/7886 ... hile-loop/