Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Switch vs if else

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends
Actually i have hundreads of cases , out of them multiple can be true ... my confusion is that wheather i shud use switch of if ... can somebody please tell me .
tx
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jolly,
I would suggest u to use switch block for that matter.
If u use if, then the code will look very messy, so it's better to use switch block.
-------------
Nayan.
 
Jolly Ag
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Nayan
but performance wise which is better choice
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Even as performance wise, I would say switch is the better option, 'coz switch statements will have break statements which can rule out the other possibilities. But for u, I don't know whether this will help or not because u have multiple true cases.
Regards
Nayan.
 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would, of course, depend on your application and the data (rule of thumb KYD. Know Your Data)and break it down into sections where the cases could be multiples. If multiple options can be taken, then you may have to use IF blocks to get better control of the flow. No, it isn't pretty, but 100+ lines of case/break statements tends to be messy as well.
My sugestion would be to use IF logic to group the incomming data with a specific set of "cases" then use a switch to get the exact option you need at the time. You should actually be able to better streamline your code by doing this.
Just because you have the ability to use a "slick" trick (like Switch / Case) to make something run or look cool doesn't mean it will work in all cases. Especially when you have to MAINTAIN the code at a later date (or worse, someone else has to do it... good way to make enemies ).
Hope this helps...
[ January 09, 2003: Message edited by: Sam Smoot ]
 
Jolly Ag
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tx. Sam
I suppose if will be better rather than switch
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really have hundreds of cases, possibly neither if nor switch are the best solution. Can you give more details on what you want to accomplish?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might consider reading this topic on a similar concern. It suggests some alternatives to either that could be used in SOME cases.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that readability is usually more important than performance, and switch usually wins here, IMO. And if we only consider differences in performance between these two:
FYI, in many cases a switch statement gets implemented by the compiler as a series of if statements, organized to be pretty efficient. You may be able to do better by hand, but often not. In many cases the compiler uses a jump table, which can be substantially more efficient than a series of if statements when there's a large number of options. In some cases hand-coded if statements can be more efficient than what the compiler will create for you from a switch - particularly if one of the options is much more common than the others; you can test for that first and save time. (It's possible Hotspot can also optimize this sort of thing; dunno.) Usually I'd be pretty confident in the compiler's ability to generate efficient code from a switch statement.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic