Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

When I first started to learn programming I remember having an argument with my teacher about If Else statements. I was arguing that:

 if {
      ...
 } else if {
      ...
 } ...

is basically the same as:

 if {
      ...
 else {
      if {
           ...
      } else {
           if {
                ...
           } else {
                ...
           }
      }
 } 

but he said they were completely different. After a while of arguing I accepted they are different. Now, over the years I have come back to this and looked over them to see the apparent major difference but for the life of me I cannot see it.

Can someone please clarify why exactly they are different (apart from the obvious nesting)

share|improve this question
1  
Thanks guys. I guess it was probably a miscommunication between my teacher and I. Also as I am new to this part of the site does anyone want to give me a few vote ups so i can give the lovely people who commented a vote up (need 15 rep) –  Skepi Feb 4 '13 at 12:08
    
Welcome to Programmers.SE. I do hope you enjoy your stay with us :-) –  Deco Feb 4 '13 at 12:18
    
Some programming languages have some kind of elseif keyword. :-) –  Martin Schröder Feb 5 '13 at 11:06

4 Answers 4

up vote 10 down vote accepted

They are logically equivalent and achieve the same thing, it's just a different way to format the code. The difference generally comes down to readability of your code. I find nested if statements harder to read personally and try to avoid them.

These statements are the same:

if (condition1)
  //block 1
else if (condition2)
  //block 2

if (conditon1)
   //block 1
else
    if (condition2)
        //block 2

Another difference comes into play (readability wise, remember - they're the same once compiled) is where you have nested ifs without elses, as they then become equivalent to and statements:

if (condition1)
    if(condition2)
       //do something

Is the same as:

if (condition1 && condition2)
    //do something
share|improve this answer
4  
It is really easy to get lost in nested if statements. Also possibly a sign that you haven't really thought through your logic. When you find yourself wanting to write a nested if, take a minute and see if you could format the code in a cleaner fashion. –  Skip Huffman Feb 4 '13 at 12:47

I'll be writing about the syntactical difference, since both forms are semantically equivalent (i.e. both programs compute the same result for any possible input).

An if-else-statement will be represented in the abstract syntax tree (AST) as a node with three children. One is the condition, one is the statement to be executed if the conditions evaluates to true and one is the statement that is to be executed if the conditions evaluates to false. The important observation here is that both statements are single statements. So what do you do if you want to execute more than one statement? You use a block and blocks are marked with curly braces in Java. A block statement may contain arbitrarily many statements (each separated by a semicolon).

So in your first example the parser will (aside from complaining about the missing condition) interpret your input as an if-node with a block statement as "then"-statement and a second if-statement as else-statement. In the second example the parser will recognize again an if-statement with a block as "then"-statement and a second block statement (with just a single if-statement in it) as else-statement.

share|improve this answer

Logically the two if-else statements are equal. They will have the same result.

The difference is in their semantics. By writing

 if(condition 1) {
 ...
 } else if(condition 2) {
 ...
 else if(condition n) {
 ...
 }

you mean that condition 1, ... condition n are equal (they refer to the same condition or state).

Statements like

 if(condition) {
 ...
 } else {
 // nested if-else statements here
 ... 
 }

assume that condition has only two equivalent states (for example if(x > 0) { } else {}).

When you need two choose between the two, the priority should be readability, and the second statement is rarely preferable.

share|improve this answer

As other users said both are logically same, but different representations are used based on your inputs. For simple stuff "if ... else if" are enough, but for complex ones nested "if ... else" would make more sense and is easily readable.

For example, lets assume we are checking if an input is string/number/special character after that we'll check if its in upper/lower case for string, positive/negative for number and some other check for special characters.

Now lets write it in both the representations.

Representation - 1

if (input is number && input is positive)
{
...
}
else if (input is number && input is negative)
{
...
}
else if (input is string && input is in uppercase)
{
...
}
else if (input is string && input is in lowercase)
{
...
}
else if (input is special-char && input is a currency-symbol)
{
...
}
else if (input is special-char && input is an-operator)
{
...
}
else
{
...
}

Representation - 2

if (input is number)
{
    if (number is positive)
    {
    ...
    }
    else
    {
    ...
    }
}
else if (input is string)
{
    if (string is in uppercase)
    {
    ...
    }
    else
    {
    ...
    }
}
else if (input is special-char)
{
    if (special-char is currency symbol)
    {
    ....
    }
    else
    {
    ...
    }
}
share|improve this answer
1  
Of course, if you're doing that in Java, the language offers much nicer ways to do that particular example. –  Blrfl Feb 4 '13 at 13:11
    
I agree that point :) –  Vivekanand S V Feb 4 '13 at 13:20
    
I think you're missing several levels of indentation in representation 2 compared to the OP's format... every else needs to have an extra indentation and a set of braces. –  Hellion Feb 4 '13 at 15:27
    
@Hellion I concentrated on the point rather than the indentation, your statement is valid. –  Vivekanand S V Feb 4 '13 at 15:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.