is there a simple one-line way to write an if statement? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cgx5871
Posts: 315
Joined: 26 Jul 2018, 14:02

is there a simple one-line way to write an if statement?

Post by cgx5871 » 26 Jan 2022, 23:46

Ask a simple question weakly, is there a simple one-line way to write an if statement? For example: if a=1 then gosub xxx
Last edited by cgx5871 on 27 Jan 2022, 00:39, edited 1 time in total.

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: is there a simple one-line way to write an if statement?

Post by flyingDman » 26 Jan 2022, 23:58

Code: Select all

(a=1) && a := 2
or with "else" condition

Code: Select all

a := a=1 ? 2 : ""   ; if a is = 1  then a = 2 else a is an empty string
14.3 & 1.3.7

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: is there a simple one-line way to write an if statement?

Post by amateur+ » 27 Jan 2022, 00:32

I think you may mean ? instead of &&
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

cgx5871
Posts: 315
Joined: 26 Jul 2018, 14:02

Re: is there a simple one-line way to write an if statement?

Post by cgx5871 » 27 Jan 2022, 00:40

flyingDman wrote:
26 Jan 2022, 23:58

Code: Select all

(a=1) && a := 2
or with "else" condition

Code: Select all

a := a=1 ? 2 : ""   ; if a is = 1  then a = 2 else a is an empty string
That was not what I meant
like this:
if a=1 then gosub xxx

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: is there a simple one-line way to write an if statement?

Post by flyingDman » 27 Jan 2022, 00:42

@amateur+ No, I meant &&
Run this:

Code: Select all

a = 1
(a=1) && a := 2
msgbox % a
14.3 & 1.3.7

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: is there a simple one-line way to write an if statement?  Topic is solved

Post by flyingDman » 27 Jan 2022, 00:51

You can try:

Code: Select all

gosub % a = 1 ? "label1" : "label2"
14.3 & 1.3.7

william_ahk
Posts: 481
Joined: 03 Dec 2018, 20:02

Re: is there a simple one-line way to write an if statement?

Post by william_ahk » 27 Jan 2022, 02:11

Alternatively, there's a two line way to write this:

Code: Select all

if (a=1)
    gosub xxx

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: is there a simple one-line way to write an if statement?

Post by BoBo » 27 Jan 2022, 02:50


That was not what I meant
like this:
if a=1 then gosub xxx

Code: Select all

!a::
	Random, a, 0, 1
	(a=1) ? goSub(1) : goSub(0)
	SoundBeep
	Return

goSub(x) {
	MsgBox % x
	}
JFTR

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: is there a simple one-line way to write an if statement?

Post by boiler » 27 Jan 2022, 04:08

amateur+ wrote: I think you may mean ? instead of &&
It works as an “if” condition because short-circuit evaluation is applied by && and ||.

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: is there a simple one-line way to write an if statement?

Post by amateur+ » 27 Jan 2022, 08:34

@flyingDman, @boiler, wow that's nice. I even didn't know about this trick, thanks!
Also we can use (a = 1) || a := 3 as alternatives to:
If a != 1
a := 3

or
(a != 1) && a := 3

It is strange that (a = 1) ? a := 2 also works although ternary operator is built to include false-branch too.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

Post Reply

Return to “Ask for Help (v1)”