By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,186 Members | 1,530 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,186 IT Pros & Developers. It's quick & easy.

difference between if-then-else and conditional operator?

P: 100
I'm not sure if I am using the right term but by conditional operator I mean ?:

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. }
  10. OR
  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.
Jul 11 '09 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 8,959
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.
Jul 11 '09 #2

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

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.
Jul 12 '09 #3

Expert 100+
P: 2,418
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.
Jul 12 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.