Page 1 of 1

simulating C-style three-parameter for-loop with ??=

Posted: 16 Sep 2023, 17:04
by bonobo

Code: Select all

while(i??=0, i++, i<=5)
{
	Msgbox(i)
}
Silly/useless, but I can't help it. :HeHe:

Re: simulating C-style three-parameter for-loop with ??=

Posted: 16 Sep 2023, 17:48
by andymbody
bonobo wrote:
16 Sep 2023, 17:04
Silly/useless, but I can't help it.
I don't think its silly or useless, if it were to work...

Generates an error for me (v2)
Missing operand.
Specifically: ??=0, i++, i<=5)


Should it work?

This worked

Code: Select all

i := 0
while(i++ < 5)
{
	Msgbox(i)
}

Re: simulating C-style three-parameter for-loop with ??=

Posted: 16 Sep 2023, 18:06
by bonobo
andymbody wrote:
16 Sep 2023, 17:48
bonobo wrote:
16 Sep 2023, 17:04
Silly/useless, but I can't help it.
I don't think its silly or useless, if it were to work...

Sorry, forgot to mention that ??= is (as of now) only available in alpha v2.1-alpha.2+

https://www.autohotkey.com/docs/alpha/Variables.htm#maybe-assign
[v2.1-alpha.2+]: x ??= y is shorthand for x := x ?? y. y is evaluated and assigned to x only if x is unset. This provides a succinct way to initialize a variable, property or array/map element only if it didn't already have a value.

Re: simulating C-style three-parameter for-loop with ??=

Posted: 16 Sep 2023, 18:09
by flyingDman

Re: simulating C-style three-parameter for-loop with ??=

Posted: 16 Sep 2023, 18:37
by andymbody
Nice... user defined counter, with step. Thanks!
Andy

Re: simulating C-style three-parameter for-loop with ??=

Posted: 16 Sep 2023, 18:40
by andymbody
bonobo wrote:
16 Sep 2023, 18:06
Sorry, forgot to mention that ??= is (as of now) only available in alpha v2.1-alpha.2+
Thank for the clarification! And the tip...

Re: simulating C-style three-parameter for-loop with ??=

Posted: 16 Sep 2023, 18:45
by flyingDman
andymbody wrote:
16 Sep 2023, 18:37
Nice... user defined counter, with step. Thanks!
Andy
Thank @Descolada ! (I would'nt have been able to come up with that!! :D )

Re: simulating C-style three-parameter for-loop with ??=

Posted: 16 Sep 2023, 19:12
by iseahound
Nice! Here's another C-style idiom: the downto operator denoted by -->

Code: Select all

x := 10
while (x --> 0) ; x downto 0
   msgbox x
Works perfectly in AutoHotkey too!

Re: simulating C-style three-parameter for-loop with ??=

Posted: 16 Sep 2023, 20:13
by flyingDman
Wow! Not documented i guess (could not find it)!

Re: simulating C-style three-parameter for-loop with ??=

Posted: 16 Sep 2023, 21:22
by andymbody
flyingDman wrote:
16 Sep 2023, 18:45
Thank @Descolada ! (I would'nt have been able to come up with that!! :D )
Will do... me neither... I struggle to comprehend lambdas. I probably should try to use them more so they become familiar.

Re: simulating C-style three-parameter for-loop with ??=

Posted: 11 Oct 2023, 09:10
by vmech
andymbody wrote:
16 Sep 2023, 17:48
bonobo wrote:
16 Sep 2023, 17:04
Silly/useless, but I can't help it.
I don't think its silly or useless, if it were to work...

Generates an error for me (v2)
Missing operand.
Specifically: ??=0, i++, i<=5)


Should it work?

This worked

Code: Select all

i := 0
while(i++ < 5)
{
	Msgbox(i)
}

Code: Select all

#requires Autohotkey v2.0
while(i := i ?? 0, i++, i <= 5)
{
	msgbox(i)
}

Re: simulating C-style three-parameter for-loop with ??=

Posted: 11 Oct 2023, 18:42
by ntepa

Code: Select all

while x --> 0
is really just:

Code: Select all

while (x--) > 0

Re: simulating C-style three-parameter for-loop with ??=

Posted: 12 Oct 2023, 09:28
by iseahound
Actually if you reverse the arrow you can control the speed:

1x speed

Code: Select all

x := 100
while (0 <-- x) ; x downto 0
   MsgBox x
5x speed!!!

Code: Select all

x := 100
while (0 <---------- x) ; x downto 0 but 5 times as fast
   MsgBox x

Re: simulating C-style three-parameter for-loop with ??=

Posted: 12 Oct 2023, 09:52
by andymbody
iseahound wrote:
12 Oct 2023, 09:28
Actually if you reverse the arrow you can control the speed:
What kind of wizardry is this?

Re: simulating C-style three-parameter for-loop with ??=

Posted: 13 Oct 2023, 10:10
by vmech
andymbody wrote:
12 Oct 2023, 09:52
iseahound wrote:
12 Oct 2023, 09:28
Actually if you reverse the arrow you can control the speed:
What kind of wizardry is this?
No any «magic». Just a consistent decreasing of variable's value. Such as:

Code: Select all

while ( 0 < (--x, --x, --x, --x, --x) )

Re: simulating C-style three-parameter for-loop with ??=

Posted: 13 Oct 2023, 10:27
by andymbody
vmech wrote:
13 Oct 2023, 10:10

Code: Select all

while ( 0 < (--x, --x, --x, --x, --x) )
Ah... thanks

Re: simulating C-style three-parameter for-loop with ??=

Posted: 13 Oct 2023, 17:31
by lexikos
The C construct for (initializer; condition; step) evaluates step only after the first iteration, whereas while(i??=0, i++, i<=5) evaluates i++ before the first iteration, therefore starting at 1 rather than the value set by the initializer. The C construct also evaluates the initializer regardless of whether some variable is initialized or not, whereas while(i??=0... is prone to error if you happen to reuse the variable name "i" without considering its prior value.