473,382 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

difference between if-then-else and conditional operator?

MrPickle
100 100+
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.
Jul 11 '09 #1
3 16237
Banfa
9,065 Expert Mod 8TB
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
MrPickle
100 100+
So it doesn't effect the overall performance, just the clarity of the code?
Thanks (:

P.S:
@Banfa
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
donbock
2,426 Expert 2GB
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

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

Similar topics

0
by: Luc Martineau | last post by:
Hello When we create a table, what is the difference between the REFERENCE constraint on column and the FOREIGN KEY constraint on table ? I guess that the FOREIGN KEY constraint assures us...
3
by: Matthet | last post by:
Hi, Is there any difference if I write sth like this: <xsd:simpleType name="Name"> <xsd:restriction base="xsd:string"> <xsd:minLength value="1"> <xsd:maxLength value="10">...
3
by: saiho.yuen | last post by:
Hi, Is anyone know what is the difference between Location.href and Window.location.href Thanks you very much:) Saiho
2
by: Peter | last post by:
Is there any difference between 'New' and 'CreateObject' in automation? As in..... Dim xlobj as Excel.application Set xlobj = New Excel.Application or Set xlobj =...
3
by: fdan4817 | last post by:
I read an earlier thread about a Java developer switching to .Net with much interest. It still has not resolved some questions that I am wrestling with. I am new to Windows programming (other...
4
by: Harro de Jong | last post by:
(absolute beginner here, sorry if this seems basic) Section 7.10 of 'How to Think Like a Computer Scientist' contains this discussion of string.find and other string functions: (quote) We can...
2
by: Carl Johansson | last post by:
Except for the syntax, what is the difference, if any, between this... /* ---------------------------------------------------------------------- */ private void Form1_Paint(object sender,...
45
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
34
by: Mark Sullivan | last post by:
What is the difference between the extensions *.hxx and just *.h for header files ? Can they co-exist? Mark
1
by: m konstantakis | last post by:
#include <stdio.h> int main(void) { int currentprice,upcomingprice; int currentprice1,currentprice2,currentprice3; int upcomingprice1,upcomingprice2,upcomingprice3; int difference; float...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.