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."))))))