• Post Reply Bookmark Topic Watch Topic
  • New Topic

if else if statement without else  RSS feed

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Is something like this acceptable, or should I always include else?

if () {

}
else if () {

}
else if () {

}
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can be very sure that those are the only conditions, why not?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is no problem .
We usually do like this.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any time something depends on "if you can be very sure...", a little bell goes off in a good programmer's head.

Leave off the final "else" if, and only if, there are some cases where nothing at all should happen. If this isn't the case, then what's the point of the last "if"? If there are no cases not covered by the three "if" conditions, then the last "if" can be omitted without changing the code, right?
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many of us have written:

and then had it show up in production. Me for one. Trust me, it can happen.

Extra points for the TV show episode with that title.
[ July 24, 2007: Message edited by: Stan James ]
 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for the replies!
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An old friend once counselled me ... "don't ever test for a condition that you don't know how to handle!".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!