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:
InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name If answer < georgf MsgBox, Your name is stupid. Else If answer < normap Msgbox, Your name ROCKsORZ! Else If answer < tog Msgbox, Your name is totally wimpy. 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)
InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name /* If answer < georgf MsgBox, Your name is stupid. Else If answer < normap Msgbox, Your name ROCKsORZ! Else If answer < tog Msgbox, Your name is totally wimpy. 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
InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name /* If answer < georgf MsgBox, Your name is stupid. Else If answer < normap Msgbox, Your name ROCKsORZ! Else If answer < tog Msgbox, Your name is totally wimpy. Else MsgBox, Your name is nothing special. */ MsgBox % ((answer < "georgf") ? () : ())Next, put the contents of the first Block inside the second set of () parentheses.
InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name /* If answer < georgf MsgBox, Your name is stupid. Else If answer < normap Msgbox, Your name ROCKsORZ! Else If answer < tog Msgbox, Your name is totally wimpy. Else MsgBox, Your name is nothing special. */ MsgBox % ((answer < "georgf") ? (MsgBox, Your name is stupid.) : ())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.
InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name /* If answer < georgf MsgBox, Your name is stupid. Else If answer < normap Msgbox, Your name ROCKsORZ! Else If answer < tog Msgbox, Your name is totally wimpy. Else MsgBox, Your name is nothing special. */ MsgBox % ((answer < "georgf") ? ("Your name is stupid.") : ( If answer < normap Msgbox, Your name ROCKsORZ! Else If answer < tog Msgbox, Your name is totally wimpy. 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.
InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name /* If answer < georgf MsgBox, Your name is stupid. Else If answer < normap Msgbox, Your name ROCKsORZ! Else If answer < tog Msgbox, Your name is totally wimpy. Else MsgBox, Your name is nothing special. */ MsgBox % ((answer < "georgf") ? ("Your name is stupid.") : ( (() ? () : ()) If answer < normap Msgbox, Your name ROCKsORZ! Else If answer < tog Msgbox, Your name is totally wimpy. Else MsgBox, Your name is nothing special. ))Have you figured out what do do? Wash, rinse, repeat...
InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name /* If answer < georgf MsgBox, Your name is stupid. Else If answer < normap Msgbox, Your name ROCKsORZ! Else If answer < tog Msgbox, Your name is totally wimpy. Else MsgBox, Your name is nothing special. */ MsgBox % ((answer < "georgf") ? ("Your name is stupid.") : ( ((answer < "normap") ? ("Your name ROCKsORZ!") : ( If answer < tog Msgbox, Your name is totally wimpy. Else MsgBox, Your name is nothing special. )) ))...And again...
InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name /* If answer < georgf MsgBox, Your name is stupid. Else If answer < normap Msgbox, Your name ROCKsORZ! Else If answer < tog Msgbox, Your name is totally wimpy. Else MsgBox, Your name is nothing special. */ MsgBox % ((answer < "georgf") ? ("Your name is stupid.") : ( ((answer < "normap") ? ("Your name ROCKsORZ!") : ( (() ? () : ()) If answer < tog Msgbox, Your name is totally wimpy. 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
InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name /* If answer < georgf MsgBox, Your name is stupid. Else If answer < normap Msgbox, Your name ROCKsORZ! Else If answer < tog Msgbox, Your name is totally wimpy. Else MsgBox, Your name is nothing special. */ MsgBox % ((answer < "georgf") ? ("Your name is stupid.") : ( ((answer < "normap") ? ("Your name ROCKsORZ!") : ( ((answer < "tog") ? ("Your name is totally wimpy.") : ("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.
InputBox, answer , Is anybody out there?, What is your name?, , , , , , , , Your Name MsgBox % "Your name " ((answer < "georgf") ? ("is stupid.") : ( ((answer < "normap") ? ("ROCKsORZ!") : ( ((answer < "tog") ? ("is totally wimpy.") : ("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 is used in command arguments and how it can be used in expressions.
Feedback welcome! --- [VxE]