Variable value for unlimited loop?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
matrix1108
Posts: 21
Joined: 20 Nov 2021, 07:49

Variable value for unlimited loop?

Post by matrix1108 » 30 Jan 2023, 20:59

Hi,

I have a loop that goes like this:

Code: Select all

Loop %loop% {
some code here
}
Then from my GUI, there's a drop-down menu with number 1 thru 20, which will be the value of %loop%, e.g. the number of iterations for the loop above.
My question is, what value for %loop% will make the loop be infinite? So I know what to add to my drop-down menu, such that "infinite" is also an option.

Thanks!

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Variable value for unlimited loop?

Post by mikeyww » 30 Jan 2023, 21:13

Hello,

If you look at the documentation, you can find the answer quickly.

Loop:
Count: How many times (iterations) to perform the loop. If omitted, the Loop continues indefinitely until a break or return is encountered. The built-in variable A_Index contains the number of the current loop iteration.

matrix1108
Posts: 21
Joined: 20 Nov 2021, 07:49

Re: Variable value for unlimited loop?

Post by matrix1108 » 30 Jan 2023, 22:24

mikeyww wrote:
30 Jan 2023, 21:13
Hello,

If you look at the documentation, you can find the answer quickly.

Loop:
Count: How many times (iterations) to perform the loop. If omitted, the Loop continues indefinitely until a break or return is encountered. The built-in variable A_Index contains the number of the current loop iteration.
Hi,

Thanks for replying! Yep, I've read that, but I can't simply omit it as sometimes I need it to have a definite value. Right now, my drop-down box for %loop% has values 1 - 20 and one 999999.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Variable value for unlimited loop?

Post by mikeyww » 30 Jan 2023, 23:47

Choices:

1. Provide a number with the loop
2. Omit a number with the loop
3. Use While
4. Use a loop with Until
5. Combine some of these

The documentation describes Loop, While, and Until, all with examples.

matrix1108
Posts: 21
Joined: 20 Nov 2021, 07:49

Re: Variable value for unlimited loop?

Post by matrix1108 » 31 Jan 2023, 16:09

mikeyww wrote:
30 Jan 2023, 23:47
Choices:

1. Provide a number with the loop
2. Omit a number with the loop
3. Use While
4. Use a loop with Until
5. Combine some of these

The documentation describes Loop, While, and Until, all with examples.
Alright. Thanks for the help!

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Variable value for unlimited loop?

Post by DuckingQuack » 31 Jan 2023, 16:23

@mikeyww would something like:

Code: Select all

Loop {
Code here
} Until a_index = Var1
function as intended since index will never be zero? Var1 being the gui input.
Best of Luck,
The Duck

User avatar
andymbody
Posts: 867
Joined: 02 Jul 2017, 23:47

Re: Variable value for unlimited loop?

Post by andymbody » 31 Jan 2023, 16:49

DuckingQuack wrote:
31 Jan 2023, 16:23
would something like:
As mike pointed out... this is an infinite loop

Code: Select all

Loop
{
Tooltip % A_Index
}
But if you feel that you must use Until...

To create an infinite loop, you can use anything you like as long as the result is false.

These do same thing as infinite loop above
Until false
Until 0
Until !A_Index
Until ISaySo
Until PigsFly

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Variable value for unlimited loop?

Post by DuckingQuack » 31 Jan 2023, 17:27

@andymbody I’m not the OP and the OP is looking for the ability to set the number of loops as well as have an option for infinite loops. Being condescending is unnecessary as I was also trying to offer help.
If you read the thread, you’ll notice that mike suggested using until and I was more or less asking two things, if index is ever zero and if it is and that is the until qualifier, would the loop even start.
Best of Luck,
The Duck

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Variable value for unlimited loop?

Post by mikeyww » 31 Jan 2023, 17:49

Code: Select all

#Requires AutoHotkey v1.1.33
Gui Font, s10
Gui Add, DropDownList, w230 vmax   , 1|2|3|Infinite
Gui Add, Button      , wp   Default, Loop
Gui Show,, Loop
Return

ButtonLoop:
Gui Submit
Loop {
 Send x
} Until (A_Index = max)
Return

Esc::ExitApp
DuckingQuack has the correct answer. For an infinite loop, one can use any Until value such that the A_Index never equals that value. I used the string "Infinite" in this script, but it could alternatively be 0, -1, or any string or number that is not a positive integer.

1. The Loop will start because a loop using the Loop command always starts when a count is not provided (or is provided and is a positive integer). The loop is skipped entirely whenever the variable is blank or contains a number less than 1 (I would say: whenever the variable is null or is not a positive integer).

2. A_Index is a read-only built-in variable, and is never zero inside a loop. The Until qualifier is considered part of the loop in this regard.

3. Although While assesses its condition before iterating, Loop assesses any Until condition after iterating.
Last edited by mikeyww on 31 Jan 2023, 18:11, edited 6 times in total.

User avatar
andymbody
Posts: 867
Joined: 02 Jul 2017, 23:47

Re: Variable value for unlimited loop?

Post by andymbody » 31 Jan 2023, 18:00

DuckingQuack wrote:
31 Jan 2023, 17:27
No intention of condescending... sorry if it sounded that way... my bad (was intended to be humorous, but failed). And as you pointed out, I did not notice who the OP was using my phone. So my apologies for that as well. I should have kept quiet.

Post Reply

Return to “Ask for Help (v1)”