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

Put simple Tips and Tricks that are not entire Tutorials in this forum
bonobo
Posts: 76
Joined: 03 Sep 2023, 20:13

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

16 Sep 2023, 17:04

Code: Select all

while(i??=0, i++, i<=5)
{
	Msgbox(i)
}
Silly/useless, but I can't help it. :HeHe:
User avatar
andymbody
Posts: 924
Joined: 02 Jul 2017, 23:47

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

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)
}
bonobo
Posts: 76
Joined: 03 Sep 2023, 20:13

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

16 Sep 2023, 18:06

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.
User avatar
andymbody
Posts: 924
Joined: 02 Jul 2017, 23:47

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

16 Sep 2023, 18:37

Nice... user defined counter, with step. Thanks!
Andy
User avatar
andymbody
Posts: 924
Joined: 02 Jul 2017, 23:47

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

16 Sep 2023, 18:40

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...
User avatar
flyingDman
Posts: 2825
Joined: 29 Sep 2013, 19:01

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

16 Sep 2023, 18:45

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 )
14.3 & 1.3.7
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

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

16 Sep 2023, 19:12

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!
User avatar
flyingDman
Posts: 2825
Joined: 29 Sep 2013, 19:01

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

16 Sep 2023, 20:13

Wow! Not documented i guess (could not find it)!
14.3 & 1.3.7
User avatar
andymbody
Posts: 924
Joined: 02 Jul 2017, 23:47

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

16 Sep 2023, 21:22

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.
vmech
Posts: 361
Joined: 25 Aug 2019, 13:03

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

11 Oct 2023, 09:10

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)
}
Please post your script code inside [code] ... [/code] block. Thank you.
ntepa
Posts: 432
Joined: 19 Oct 2022, 20:52

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

11 Oct 2023, 18:42

Code: Select all

while x --> 0
is really just:

Code: Select all

while (x--) > 0
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

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

12 Oct 2023, 09:28

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
User avatar
andymbody
Posts: 924
Joined: 02 Jul 2017, 23:47

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

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?
vmech
Posts: 361
Joined: 25 Aug 2019, 13:03

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

13 Oct 2023, 10:10

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) )
Please post your script code inside [code] ... [/code] block. Thank you.
User avatar
andymbody
Posts: 924
Joined: 02 Jul 2017, 23:47

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

13 Oct 2023, 10:27

vmech wrote:
13 Oct 2023, 10:10

Code: Select all

while ( 0 < (--x, --x, --x, --x, --x) )
Ah... thanks
lexikos
Posts: 9625
Joined: 30 Sep 2013, 04:07
Contact:

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

13 Oct 2023, 17:31

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.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests