3

What is the difference between:

if      (expr1) {stmt}
else if (expr2) {stmt}
else if (expr3) {stmt}    
else            {stmt}

And the same code block written as:

if (expr1) {stmt}
if (expr2) {stmt}
if (expr3) {stmt}    
else       {stmt}
2
  • 3
    Homework? Think logically about what else means, and what the difference should be.
    – Chad
    Sep 18, 2011 at 22:38
  • I think one of the differences is in the error messages.
    – user541686
    Sep 18, 2011 at 22:39

7 Answers 7

10

In the first one, each block of statements is mutually exclusive; the structure guarantees that exactly one of them will get executed.

This is not true for the second one. Consider:

if (a == 2) { /* blah */ }
if (a == 3) { /* blah */ }
if (a < 5)  { /* blah */ }

If a == 2, then two of the blocks will get executed.

2
  • Does that mean that in the first code block, say one of the if or else-if statement is true, the program will not check the rest of the if-else statements' conditions? I mean will it be logically equivalent to a sequence of switch cases, where each case is followed by a break statement? Sep 18, 2011 at 22:42
  • One situatuation where you WANT to use a switch with multiple case/break but you CAN'T is when the case label is not a constant numeric but a variable or an object (like a string) so you must do if (s == "Hello") {...} else if (s == "Goodbye") {} else if (s == "Run") {...} else {...}
    – franji1
    Sep 18, 2011 at 23:31
1

Here's another good example to see how this works.

This example will print "FirstSecond":

if(1) {
    printf("First");
}
if(1) {
    print("Second");
}

This just prints "First":

if(1) {
    printf("First");
}
else if(1) {
    print("Second");
}
1
  • Oh this is a nice example! Thanks! Sep 18, 2011 at 22:48
0

In your first code, at most ONE of the blocks can be executed.

In the second code, all of the blocks could be executed. (Except for the last else-thing.)

1
  • In the first case, exactly one will be executed. In the second, not all of the blocks could be executed. Exactly one of the last two are.
    – flight
    Sep 18, 2011 at 22:43
0

In the first case exactly one of the blocks will be executed. In the second, the first and second blocks may or may not be executed and exactly one of the last two blocks will be executed.

0

For the best performance choose a if else (if possible).

You can also use a switch case statement.

0
0

For example if condition1 == true and condition2 == true the first block(else id) will execute only some statements#1 and the second block would execute both some statements#1 and some statements#2. When you use else if the program would stop matching next conditions after first matched. It would be slightly faster on runtime if conditions are exclusive(i'm not sure if it's right word).

0

With:

if      (expr1) {stmt1}
else if (expr2) {stmt2}
else if (expr3) {stmt3}    
else            {stmt4}

One and only one of the statements can be executed.


With:

if (expr1) {stmt}

if (expr2) {stmt}

if (expr3) {stmt}    
else       {stmt}

Either, both, or none of the first and second sections will be executed. In the last if-else section, either stmt3 or stmt4 will be executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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