AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[VxE]'s := guide ? ( to the ternary ) : ( operator ) .

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
[VxE]



Joined: 07 Oct 2006
Posts: 1129

PostPosted: Sun Mar 16, 2008 10:49 pm    Post subject: [VxE]'s := guide ? ( to the ternary ) : ( operator ) . Reply with quote

btw: I'm invoking
The Forums Index Page wrote:
...Post helpful tips and tricks.
to post this here. {also this is my 500th post.}

What is it?!
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:
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)
Code:
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
Code:
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.
Code:
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.
Code:
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.
Code:
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...
Code:
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...
Code:
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
Code:
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.
Code:
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]
_________________
My Home Thread
More Common Answers: 1. It's in the FAQ 2. Ternary ( ? : ) guide 3. Post code with [code][/code] tags
Back to top
View user's profile Send private message
Crummy
Guest





PostPosted: Mon Mar 17, 2008 3:42 am    Post subject: Reply with quote

Hey! Wait a second! My name is not stupid >Sad
Back to top
TodWulff



Joined: 29 Dec 2007
Posts: 99

PostPosted: Mon Mar 17, 2008 4:02 am    Post subject: Re: [VxE]'s := guide ? ( to the ternary ) : ( operator ) . Reply with quote

[VxE] wrote:
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.

Laughing ROFL! That's good. Thanks for the giggles. Thanks, also, for the nice primer! Take care.

-t
Back to top
View user's profile Send private message
Rhys



Joined: 17 Apr 2007
Posts: 722
Location: Florida

PostPosted: Mon Mar 17, 2008 4:23 am    Post subject: Reply with quote

I'm very interested if this increases code execution speed over several if/else lines. Thanks for the primer - Very well put together!
_________________
[Join IRC!]
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2558
Location: Australia, Qld

PostPosted: Mon Mar 17, 2008 5:39 am    Post subject: Reply with quote

Rhys, in [VxE]'s example, the ternary version is actually slower. Both are under 10”s, though, so it isn't really an issue. Readability is, though... this is one of those cases where ternary makes it less readable.

Generally ternary is more efficient in simple expressions, and less efficient in more complex ones.
Back to top
View user's profile Send private message
Oberon



Joined: 18 Feb 2008
Posts: 458

PostPosted: Mon Mar 17, 2008 10:20 am    Post subject: Reply with quote

In higher level languages like C# and PHP I usually use associative arrays for lookup tables and switch/case as an alternative to stacking If statements. Ternary operators shouldn't be nested more than three times deep, and if it has to be this much for numerical values only. Ifs are faster because machine code processes them in the same form, giving the compiler more room to optimize.

Nice article by the way. As a hobbyist programmer there are many gaps in my knowledge so I always find it interesting to read stuff like this.
Back to top
View user's profile Send private message
BoBoš
Guest





PostPosted: Mon Mar 17, 2008 10:54 am    Post subject: Reply with quote

Cool [HowTo]. Thx. Very Happy
I'd like to see something similar for 'continuation sections'. *wishgrin*

Especially when we'll set up real loooooooooong lines, like those shown above, it would make sense to use the continuation option to get a better layout/visability, right?

I saw it sometimes used with DllCall()s ... Cool
Back to top
[VxE]



Joined: 07 Oct 2006
Posts: 1129

PostPosted: Tue Mar 18, 2008 10:12 pm    Post subject: Reply with quote

Thx for the feedback everyone Very Happy I thought of something that would improve this guide but that I probably would mess up if I did it myself:

Examples of commonly seen ternary usages !

I would like to keep a growing list of instances where the ternary operator is preferred for readability. For example:
Code:
Hotkey:: SetTimer, Label, % ((Bool := !Bool) ? (Number) : ("Off"))


Maybe some more examples like this with brief explanations would help new users learn from more examples, and would improve the search-hit-keyword spread for this thread.
_________________
My Home Thread
More Common Answers: 1. It's in the FAQ 2. Ternary ( ? : ) guide 3. Post code with [code][/code] tags
Back to top
View user's profile Send private message
poetbox



Joined: 07 Jan 2007
Posts: 59

PostPosted: Mon May 05, 2008 5:22 am    Post subject: Re: [VxE]'s := guide ? ( to the ternary ) : ( operator ) . Reply with quote

[VxE] wrote:
btw: I'm invoking
The Forums Index Page wrote:
...Post helpful tips and tricks.
to post this here. {also this is my 500th post.}

Well Done! Surprised Laughing Very Happy Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group