Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

For loop question


  • Please log in to reply
11 replies to this topic
trante
  • Members
  • 11 posts
  • Last active: Jul 25 2013 06:10 AM
  • Joined: 15 May 2011
Hello,
How can i do this in autohotkey?
Thank you

for i=1 to 12
{
MsgBox, hello! %i%
}

Help file didn't help:
For Key [, Value] in Expression

Solar
  • Members
  • 345 posts
  • Last active: Jan 15 2012 08:11 PM
  • Joined: 03 May 2009
Loop 12

	MsgBox, hello! %A_Index%


trante
  • Members
  • 11 posts
  • Last active: Jul 25 2013 06:10 AM
  • Joined: 15 May 2011

Loop 12
	MsgBox, hello! %A_Index%


You mean that
"for i=5 to 12" is equal to "Loop 8"

But i use variable "i" in my code.
for example
"if i<7
then ....
elseif i>10
then ....
end"

Solar
  • Members
  • 345 posts
  • Last active: Jan 15 2012 08:11 PM
  • Joined: 03 May 2009

You mean that
"for i=5 to 12" is equal to "Loop 8"

But i use variable "i" in my code.
for example
"if i<7
then ....
elseif i>10
then ....
end"


2 examples:

i := 5
Loop 8 { ; i + 8 (12)
	if (i < 7)
		continue ; then ...
	else if (i > 10)
		continue ; then ...
	i += 1 ; change increment
}

loop 8 {
	i := A_Index+4 ; 5 start with + 1 increment
	if (i < 7)
		continue ; then ...
	else if (i > 10)
		continue ; then ...
}


trante
  • Members
  • 11 posts
  • Last active: Jul 25 2013 06:10 AM
  • Joined: 15 May 2011
Thank you very much for help

Solar
  • Members
  • 345 posts
  • Last active: Jan 15 2012 08:11 PM
  • Joined: 03 May 2009
You can also define variables in the Loop statement by turning it into an expression. This would save you a line:

Loop % (i := 5) + 3 ; loops 5+3 times, i = 12 at the break
	i += 1


trante
  • Members
  • 11 posts
  • Last active: Jul 25 2013 06:10 AM
  • Joined: 15 May 2011
Thank you Solar for helps.
But a "for-loop" would be very helpful if autokey had it.
Regards

Solar
  • Members
  • 345 posts
  • Last active: Jan 15 2012 08:11 PM
  • Joined: 03 May 2009

Thank you Solar for helps.
But a "for-loop" would be very helpful if autokey had it.
Regards

Why? Loop is essentially the same without an inline increment.

trante
  • Members
  • 11 posts
  • Last active: Jul 25 2013 06:10 AM
  • Joined: 15 May 2011

Thank you Solar for helps.
But a "for-loop" would be very helpful if autokey had it.
Regards

Why? Loop is essentially the same without an inline increment.


Well that's my habit from other languages :) To express increment in the beginning of loop. For this reason, i don't like "while-do" loops what makes inside increments compulsive :)

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

But a "for-loop" would be very helpful if autokey had it.

... based on this post:
for i in list.new(5,12)

   MsgBox, %i%





class List {

   New(Start, End, Step=1) {

      return {1:Start-Step, 2:End, 3:Step, base:this}

   }

   _NewEnum() {

      return this

   }

   Next(ByRef var) {

      return (var := this[1] := this[1] + this[3]) <= this[2]

   }

}


OhWow
  • Guests
  • Last active:
  • Joined: --

for i in list.new(5,12)
MsgBox, %i%


class List {
New(Start, End, Step=1) {
return {1:Start-Step, 2:End, 3:Step, base:this}
}
_NewEnum() {
return this
}
Next(ByRef var) {
return (var := this[1] := this[1] + this[3]) <= this[2]
}
}

If I got to the point that I wanted a for loop that badly (or classes or OOP in general), I'd use another language that does them naturally rather than that mess :(

tomy
  • Guests
  • Last active:
  • Joined: --
Here's a really unelegant solution (but it does let you do the for loop).

Just put this somewhere in the code:
for(var,start,step,end,sub)
{
	%var% := start
	count := end-start+1
	loop %count%
	{
		tooltip 
		gosub % sub
		%var%:=%var%+step
	}
}

example:
for("i",5,1,12,"dothis")
return

dothis:
MsgBox % i
return

but like other people have mentioned, its nothing a normal loop can't do. although I agree with you that its easier to transfer the skill from other languages.