The Ternary Operator ( x ? y : z )

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

The Ternary Operator ( x ? y : z )

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] [Download] GeSHi © Codebox Plus

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] [Expand] [Download] GeSHi © Codebox Plus



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] [Expand] [Download] GeSHi © Codebox Plus



Next, put the contents of the first Block inside the second set of () parentheses.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



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] [Expand] [Download] GeSHi © Codebox Plus



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] [Expand] [Download] GeSHi © Codebox Plus



Have you figured out what do do? Wash, rinse, repeat...

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



...And again...

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



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] [Expand] [Download] GeSHi © Codebox Plus



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] [Download] GeSHi © Codebox Plus

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: 490
Joined: 30 Sep 2013, 09:34

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

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: 2320
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

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

01 Oct 2013, 02:22

[AHK] 1.1.29.01 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
Alibaba
Posts: 472
Joined: 29 Sep 2013, 16:15
Location: Germany

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

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

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 3 guests