Thread: Usefulness of the "else if" statement

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    There is no real "else if" statement in C. Consider the following:

    Code:
    if(x == 0) printf("Warning!  X is 0!\n");
    That code is perfectly valid (assuming x has been previously declared, etc.) because an if statement, if its condition is true, will execute the next statement.

    The else part of an if clause is the same way:

    Code:
    if(y != 0) printf("y is not 0\n");
    else printf("y is 0\n");
    The else part will execute only the next statement should the condition of the original if statement be false.

    So hence:

    Code:
    if(x == 0) ....
    else if(x == y) ....
    else ....
    Is really expanded like this:

    Code:
    if(x == 0)
    {
            ....
    }
    else
    {
            if(x == y)
            {
                    ....
            }
            else
            {
                    ....
            }
    }
    I rushed this, but if I didn't make a mistake, both forms are equivalent.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Imagine a very large if/else block like this:
    Code:
    if ( n == 1 ) {...}
    else if ( n == 2 ) {...}
    else if ( n == 3 ) {...}
    else if ( n == 4 ) {...}
    ...
    else if ( n == 100 ) {...}
    If n is 1, it will match the first if clause and will skip all the else if statements.

    If however, you wrote only if statements:
    Code:
    if ( n == 1 ) {...}
    if ( n == 2 ) {...}
    if ( n == 3 ) {...}
    if ( n == 4 ) {...}
    ...
    if ( n == 100 ) {...}
    Then if n is 1, it will still evaluate all 99 other if statements which is a complete waste of time.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    5
    Ah. I think I understand now. Thank you very much for the examples and the explanations. (Though, I'm way too low of a level to be able to comprehend citizen's example, but it helps that he gives me an example where "else if" is quite necessary and isn't simply a matter of efficiency)

    I was experimenting, and there's something that I want to run by you guys once more. I created a (rather sloppy) program, and found something interesting.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int x;
        printf ("Insert a number between 1-3: ");
        scanf ("&#37;d", &x);
        if (x == 1)
           printf ("x is one.");
        else if (x == 2)
           printf ("x is two.");
        else if (x == 3)
           printf ("x is three.");
        else
            printf ("x is not one, two, or three.");
        return 0;
    }
    Input: 1 --> x is one
    Input: 2 --> x is two
    Input: 3 --> x is three
    Input: 4 --> x is not one, two, or three.

    I also tried changing all of the "else if" statements to "if" statements, and it gave me different results:
    Input: 1 --> x is one.x is not one, two, or three.
    Input: 2 --> x is two.x is not one, two, or three.
    Input: 3 --> x is three.
    Input: 4 --> x is not one, two, or three.

    It seemed really peculiar, but when I looked at MacGyver's explanation of how ifs/thens are expanded, it makes sense. It goes something like this, right?
    Code:
    if(x == 0) ....
    else if(x == y) ....
    else if(x == z) ....
    else ....
    expands to...
    Code:
    if (x == 0) ....
    {
          ....
    }
    else
    {
        if (x == y)
        {
              ....
        }
        else
        {
            if (x == z)
            {
                  ....
            }
        }
    }
    This seems consistent with my example programs. In the "else if" version, if '1' or '2' is the input, then it will skip all of else(/ifs) after it, which is why, when given '1' or '2', it gives "x is 1 (or 2)" as the output without the "x is not one, two, or three" string after it. It looks to me like a tree of if/thens. The first "if" has a branch, which is the first "else if," and this "else if" has another branch, which is another "else if," etc. However, with the "only if" version, all three "ifs" are on different parts of the tree, which would explain why it gives me an output like "x is one.x is not one, two, or three." The "only if" version evaluates all three of the conditions. With '1,' it evaluates "if (x == 1) as true, so it proceeds with that. if(x == 2) is false, so it skips it, and if(x == 3) is also false, which explains why it goes to the "else" statement. (It looks like in this scenario, the else statement belongs to the if(x == 3) branch because it comes right after it). This seems like a delicate subject because I'm so early on (You don't want to build a house on a faulty foundation, right?), so I wanted to make sure that I have my ideas on the right track.

    Again, I am very thankful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. too many "else if" statements
    By xwielder in forum C++ Programming
    Replies: 8
    Last Post: 11-10-2006, 09:34 PM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM