The Ternary Operator ( x ? y : z )

Helpful script writing tricks and HowTo's
VxE
Posts: 45
Joined: 30 Sep 2013, 10:35
Location: Simi Valley, CA

The Ternary Operator ( x ? y : z )

Post by VxE » 30 Sep 2013, 15:42

Q: What is it?!
A: The ternary operator is like an If/Else conditional, but packaged to fit inside an expression.

What does it look like?!
The ternary operator looks like a boolean expression followed by a question mark followed by something, then a colon and another something

What is it used for?!
The ternary operator collapses one or more "If/Else" blocks so that they fit inside a normal command or into an expression.

Why bother with it if I can do the same thing with "If/Else"?!
The ternary operator has a coolness factor rating of 14 units = sunglasses. Using it makes your code shorterr, cooler, and more complicated-looking.

Is there an easy way to write them?... I tried once and it I got confused.
Certainly! This is a guide to both writing and understand them...

Firstly, you should start by writing out your code the long IFfy way:

Code: Select all

InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name
If answer < georgf
MsgBox, Your name is mud.
Else If answer < normap
Msgbox, Your name ROCKsORZ!
Else If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
So... you can use the ternary operator to reduce all of those Ifs and Elses and Msgboxes down to a single Msgbox. To begin, comment out the lines that you want to convert and start a new line with the same command that is common to those If/Else blocks. Then put the string % (() ? () : ()) where the argument of interest is (that's pretty simple for a basic Msgbox, but you can use the ternary in whatever argument you want)

Code: Select all

InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name
/*
If answer < georgf
MsgBox, Your name is mud.
Else If answer < normap
Msgbox, Your name ROCKsORZ!
Else If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
*/
MsgBox % (() ? () : ())
OK, now we can start building our ternary expression. Next, find the first boolean expression and paste it into the first set of () parentheses. REMEMBER to use 'expression format' when dealing with literal strings and variables

Code: Select all

InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name
/*
If answer < georgf
MsgBox, Your name is mud.
Else If answer < normap
Msgbox, Your name ROCKsORZ!
Else If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
*/
MsgBox % ((answer < "georgf") ? () : ())
Next, put the contents of the first Block inside the second set of () parentheses.

Code: Select all

InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name
/*
If answer < georgf
MsgBox, Your name is mud.
Else If answer < normap
Msgbox, Your name ROCKsORZ!
Else If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
*/
MsgBox % ((answer < "georgf") ? (MsgBox, Your name is mud.) : ())
Have you spotted something that's missing and something that should be? Yes, because we are inside an expression, literal strings must be enclosed in "" quote marks and we don't need a second "Msgbox". So fix that and then copy the "Else" block into the third set of () parentheses.

Code: Select all

InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name
/*
If answer < georgf
MsgBox, Your name is mud.
Else If answer < normap
Msgbox, Your name ROCKsORZ!
Else If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
*/
MsgBox % ((answer < "georgf") ? ("Your name is mud.") : (
If answer < normap
Msgbox, Your name ROCKsORZ!
Else If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
))
OMG!! what have we done!! That expression is totally 'F'-ed UP!. Our "Else" block has more than just a single command... it has more "IF/Else"s in it! Have you guessed what we're going to do?... That's right, another ternary operator. Go ahead and paste another happy little (() ? () : ()) onto the line where we're working.

Code: Select all

InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name
/*
If answer < georgf
MsgBox, Your name is mud.
Else If answer < normap
Msgbox, Your name ROCKsORZ!
Else If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
*/
MsgBox % ((answer < "georgf") ? ("Your name is mud.") : ( (() ? () : ())
If answer < normap
Msgbox, Your name ROCKsORZ!
Else If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
))
Have you figured out what do do? Wash, rinse, repeat...

Code: Select all

InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name
/*
If answer < georgf
MsgBox, Your name is mud.
Else If answer < normap
Msgbox, Your name ROCKsORZ!
Else If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
*/
MsgBox % ((answer < "georgf") ? ("Your name is mud.") : ( ((answer < "normap") ? ("Your name ROCKsORZ!") : (
If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
))
))
...And again...

Code: Select all

InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name
/*
If answer < georgf
MsgBox, Your name is mud.
Else If answer < normap
Msgbox, Your name ROCKsORZ!
Else If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
*/
MsgBox % ((answer < "georgf") ? ("Your name is mud.") : ( ((answer < "normap") ? ("Your name ROCKsORZ!") : ( (() ? () : ())
If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
))
))
OK, here we are at the last one. We no longer have to nest more ternary operators, so we can go ahead and put the strings into their approprate parentheses

Code: Select all

InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name
/*
If answer < georgf
MsgBox, Your name is mud.
Else If answer < normap
Msgbox, Your name ROCKsORZ!
Else If answer < tog
Msgbox, Your name is grumpleskillspin.
Else
MsgBox, Your name is nothing special.
*/
MsgBox % ((answer < "georgf") ? ("Your name is mud.") : ( ((answer < "normap") ? ("Your name ROCKsORZ!") : ( ((answer < "tog") ? ("Your name is grumpleskillspin.") : ("Your name is nothing special."))))))
Don't forget to bring those trailing "))" close-parentheses back to the proper line. Now, this expression will work fine, however there is one little thing we can do to make it even shorter. We can bring out the part of each string that is the same ("Your name ") and put it outside of our ternary operator.

Code: Select all

InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name
MsgBox % "Your name " ((answer < "georgf") ? ("is mud.") : ( ((answer < "normap") ? ("ROCKsORZ!") : ( ((answer < "tog") ? ("is grumpleskillspin.") : ("is nothing special."))))))
And there you have it! A beautiful decorative hand-carved wooden duck decoy that you made yourself. Hopefully, those who didn't know about this shortcut will have gained a better understanding of how the ternary operator can be used in expressions.
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: The Ternary Operator ( x ? y : z )

Post by smorgasbord » 01 Oct 2013, 01:57

VxE
"The ternary operator has a coolness factor rating of 14 units = sunglasses. Using it makes your code shorterr, cooler, and more complicated-looking."

is it copy-righted? i just love it!!

including the tutorial as well :)
John ... you working ?
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: The Ternary Operator ( x ? y : z )

Post by jNizM » 01 Oct 2013, 02:22

[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Alibaba
Posts: 480
Joined: 29 Sep 2013, 16:15
Location: Germany

Re: The Ternary Operator ( x ? y : z )

Post by Alibaba » 01 Oct 2013, 11:25

great tutorial!
i wish i had read that when i started with autohotkey and was confused by the ternary operator. :)
"Nothing is quieter than a loaded gun." - Heinrich Heine
Post Reply

Return to “Tutorials (v1)”