The Ternary Operator ( x ? y : z )
Posted: 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:
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)
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
Next, put the contents of the first Block inside the second set of () parentheses.
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.
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.
Have you figured out what do do? Wash, rinse, repeat...
...And again...
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
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.
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.
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.
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 % (() ? () : ())
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") ? () : ())
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.) : ())
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.
))
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.
))
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.
))
))
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.
))
))
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."))))))
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."))))))