QUESTION: nested If and Else If statement

+1 Jeroen van der Schalie · January 20, 2016
I was wondering why you would choose to use a nested if or an Else If statement.
So I know the difference in code. I'm just wondering what the benefits of one or the other are.

Greetz,

J++

Post a Reply

Replies

Oldest  Newest  Rating
0 Nicholas Eason · January 20, 2016
They are exactly the same.

So I'm not sure if you know, but when you use an if statement, the curly braces aren't required, the same goes with the else statement. When you don't include curly braces, Java assumes you want only the program statement after that to be part of the if-statement. For example:

int salary = 20000;
if(salary == 20000){
   salary+=10000;
   System.out.println(salary);
}

The code segment above prints 30,000, as expected. However,

int salary = 30000;
if(salary == 20000)
   salary+=10000;
System.out.println(salary);

the above segment prints 30,000. Also, note that indentation is irrelevant.

So to answer your question about the benefits, there are no benefits. They are 100% the same efficiency. Personally, I prefer using "non-nested" Else If because it looks cleaner.
  • 1

Java / Android Development

106,989 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator