This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Is switchcase is better than if else Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is switchcase is better than if else" Watch "Is switchcase is better than if else" New topic
Author

Is switchcase is better than if else

Ali Asghar
Greenhorn

Joined: Sep 19, 2001
Posts: 1
Is switch case is better than if else and why?
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
Depends on what you need it for. switch is better for picking between lots of options but I think all options have to be numeric. if is boolean based, and can be nested inside other if statements, but if you have lots of options, it can get ugly. I'm not sure about the internal workings of java with these two. With if I know you have to go through every boolean check above the correct option before you get to the correct one. I suspect that switch works in such a way as to minimize the "select the correct option" branch.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I've actually read a couple of books that avoid switch all together. Since I don't have them in front of me, I can't site exactly why they do, but I think it has something to do with switch not really exhibiting OO-type behavior (kind of like goto). Anyway, I personally don't use it unless it is specifically asked for, I find using if/else statements easier in both usage and reading.
Jason
 
wood burning stoves
 
subject: Is switchcase is better than if else
 
Similar Threads
Help on Java API for accessing exchange server from java
Testing a java program
fair
GB vs the USA
Which is better "if/else" or "loop"?