Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

if..else..if  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if() {
}
else{
if() {
}
}

(or)

if() {
}
else if(){
}
Whether both are same? if yes, which one is a goog programming style?
 
Bajji Pat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are nearly the same but,

In the first case there is only one else part i.e if the IF part fails it enters the ELSE part,(there is no other alternate path) but if u want to check multiple conditions i.e u can multiple ELSE IF's where the check is made before(if at all) an ELSE part is executed.

Balaji
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first form allows you to put more into the else than a single if statement.

If you don't want to do more, I prefer the second form, because I find it to be easier to read/more expressive/less verbose.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only ones that bother me are when code formatters do this:

This kind of "right creep" conceals the notion that I'm really doing something very much like a choose case. Reason enough for me to use

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!