Ternary operator, how do I open it ? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Class1
Posts: 39
Joined: 28 Sep 2017, 02:51

Ternary operator, how do I open it ?

18 Oct 2017, 22:55

I'm having a trouble with ternary operators.

In this example it's obvious how it opens up:

Code: Select all

A::(varA = 0 ? (varA := 1) : (varA := 0))
to -->
a::
    if(varA = 0) {
        varA := 1
    } else {
        varA := 0
    }
return
But in this example I'm a bit confused:

Code: Select all

a::varA :=  bool ? 1 : 0
to -->
?????
Any help is appreciated
iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: Ternary operator, how do I open it ?  Topic is solved

18 Oct 2017, 23:37

Code: Select all

if (bool == true)
    varA := 1
else if (bool == false)
    varA := 0
return
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Ternary operator, how do I open it ?

18 Oct 2017, 23:54

isea is on to it. I'm just throwing in an example and a bit of explanation that may help you:

Code: Select all

q::
var:="Tree"
GoSub, checkvar
var:=False
GoSub, checkvar
var:=2
GoSub, checkvar
var:=0
GoSub, checkvar
var:=""
GoSub, checkvar
return

checkvar:
If (var)
MsgBox var has a value: %var%
else
MsgBox var is value null (empty) or 0 (False is a built in variable representing 0)`n Var is >%var%<
return
var is evaluated to be true or false ultimately to satisfy the If condition. If it's empty or 0, then it's False, otherwise it's true.
Class1
Posts: 39
Joined: 28 Sep 2017, 02:51

Re: Ternary operator, how do I open it ?

19 Oct 2017, 01:49

Exaskryz wrote:isea is on to it. I'm just throwing in an example and a bit of explanation that may help you:

Code: Select all

q::
var:="Tree"
GoSub, checkvar
var:=False
GoSub, checkvar
var:=2
GoSub, checkvar
var:=0
GoSub, checkvar
var:=""
GoSub, checkvar
return

checkvar:
If (var)
MsgBox var has a value: %var%
else
MsgBox var is value null (empty) or 0 (False is a built in variable representing 0)`n Var is >%var%<
return
var is evaluated to be true or false ultimately to satisfy the If condition. If it's empty or 0, then it's False, otherwise it's true.
THANKS FOR THE EXPLANATION
Class1
Posts: 39
Joined: 28 Sep 2017, 02:51

Re: Ternary operator, how do I open it ?

19 Oct 2017, 01:50

iseahound wrote:

Code: Select all

if (bool == true)
    varA := 1
else if (bool == false)
    varA := 0
return
THANKS FOR THE SOLUTION
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Ternary operator, how do I open it ?

19 Oct 2017, 04:02

Your two examples and iseahound's example expressed in different ways.

Code: Select all

;these two are equivalent
q::
varA := (varA = 0) ? 1 : 0
MsgBox, % varA
return

w::
if (varA = 0)
	varA := 1
else
	varA := 0
MsgBox, % varA
return

;==================================================

;these two are equivalent

e::
varA := bool ? 1 : 0
MsgBox, % varA
return

r::
if bool
	varA := 1
else
	varA := 0
MsgBox, % varA
return

;==================================================

;iseahound's example is slightly different to the one above
;it's actually two ternary operators in one AFAICS
;these four are equivalent

t:: 
varA := (bool = 1) ? 1 : ((bool = 0) ? 0 : varA)
MsgBox, % varA
return

y:: 
if (bool = 1)
	varA := 1
else if (bool = 0)
	varA := 0
MsgBox, % varA
return

;two redundant lines added, which make the similarity clearer
u:: 
if (bool = 1)
	varA := 1
else if (bool = 0)
	varA := 0
else
	varA := varA
MsgBox, % varA
return

i:: 
if (bool = 1) || (bool = 0)
	varA := bool
MsgBox, % varA
return

;==================================================
For your two examples, you could also use ! i.e. NOT.

Code: Select all

q::
varA := !varA
MsgBox, % varA
return

w::
varA := !!bool
MsgBox, % varA
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: hiahkforum, vanove and 145 guests