Connecting Tech Pros Worldwide Forums | Help | Site Map

difference between if-then-else and conditional operator?

MrPickle's Avatar
Member
 
Join Date: Jul 2008
Posts: 98
#1: Jul 11 '09
I'm not sure if I am using the right term but by conditional operator I mean ?:

e.g.;
statement ? value if true : value if false

What are the differences between if-then-else and ?:
Is it better to use if-then-else or will ?: suffice in quick situations?

For example, It strikes me as easier and better in the below example, but I often find with many languages that there is hidden drawbacks and/or it is frowned upon. to things so I thought I would check.

Expand|Select|Wrap|Line Numbers
  1. food = (monkey ? banana : cheese)
than this:
Expand|Select|Wrap|Line Numbers
  1. if(monkey)
  2. {
  3.    food = banana
  4. }
  5. else
  6. {
  7.    food = cheese
  8. }
  9.  
  10. OR
  11.  
  12. if(monkey)
  13.    food = banana
  14. else
  15.    food = cheese
Obviously, if-then-else would be better if there was more than one thing happening in between the braces.

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#2: Jul 11 '09

re: difference between if-then-else and conditional operator?


It's called the ternary operator (it is the only operator to take 3 parameters hence the rather generic name) or conditional expression.

The difference is the ternary operator is an expression where as an if-then-else is a statement.

The basic difference is an expression returns a value and a statement doesn't so the ternary operator can be used in places that an if statement can't.

Personally I tend to avoid using the ternary operator, I think it makes the code harder to read and has few (if any) advantages.

As to you 2 if statements I would always use the first, putting in the braces every time tends to lead to less maintenance issues.
MrPickle's Avatar
Member
 
Join Date: Jul 2008
Posts: 98
#3: Jul 12 '09

re: difference between if-then-else and conditional operator?


So it doesn't effect the overall performance, just the clarity of the code?
Thanks (:

P.S:
Quote:

Originally Posted by Banfa View Post

As to you 2 if statements I would always use the first, putting in the braces every time tends to lead to less maintenance issues.

I always tend to use the former out of my if-then-else example, I just put the other one in to clarify I knew you could do it.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#4: Jul 12 '09

re: difference between if-then-else and conditional operator?


I generally avoid the ternary operator because it doesn't seem to be used very commonly. (A positive feedback loop.) Maintainability is enhanced by avoiding uncommon or tricky code.

I suppose there is a theoretical advantage for the ternary operator in a conditional assignment. The lvalue variable only appears once in the ternary case but twice in the if-then-else case -- cutting the likelihood of typing the wrong variable name in half. I've never seen a bug of this sort so I don't consider this a compelling argument.

Another example of how I've seen the ternary operator used is for rounding a floating-point number:
Expand|Select|Wrap|Line Numbers
  1. value += (value >= 0.0) ? 0.5 : -0.5;
The ternary operator is a staple of entries in the Obfuscated C Contest; not a ringing endorsement.
Reply