-2

I was thinking the other day about the creation of "else-if" statements and why the way of its execution causes each event to be guaranteed mutually exclusive.

For example:

if(condition A)
    //condition
else if(condition B)
    //will run if condition A is false and condition B is true
else if(condition C)
    //will run if condition A is false and condition B is false and condition C is true
else if(condition D)
    //will run if all the above conditions are false and condition D is true. 

I would think it would make more sense for all the "else" statements to be checked if condition A is not true, and not just stop if either B or C are true. My intuition thinks it would be more natural to have the above code be equivalent to this:

if(condition A)
    //condition
else {
    if(condition B)
    //...
    if(condition C)
    //...
    if(condition D)
    //...
}

So therefore, why do we define else-if the way we do? Is it to circumvent unnecessary nesting of if-else statements? I just think it is ambiguous and would make sense to have it be equivalent to my second code snippet.

Edit: to clear up confusion, I completely understand that these two statements are not always equivalent. My question is primarily asking why else-if is defined such that the first statement is not always equivalent to the second statement? I'm trying to understand why else-if runs the way it does.

Edit 2: I think I finally understand the underlying essence of my question. Generally, "else" checks if the above statement is false, and if it is, it runs the statement. However, in the case of elif, it checks to see if all the above statements are false before running. This is different from the duplicate question as it asks about the nature of if-else itself, rather than its exhaustiveness.

EDIT 3: I have opened a new question which is hopefully clearer, found here.

Community
  • 1
  • 1
rb612
  • 5,280
  • 3
  • 30
  • 68
  • No one's stopping you from using the nested approach. – shmosel Jan 15 '17 at 06:37
  • 3
    If conditions B, C, and D are not mutually exclusive, the results of the execution of your code fragments will be different. – DYZ Jan 15 '17 at 06:37
  • They're not always the same though. – Maroun Jan 15 '17 at 06:38
  • Thank you for the input. Please see my edit. – rb612 Jan 15 '17 at 06:39
  • 1
    Languages based on the C syntax (like Java) don't really have an "else-if" statement. They have"if-else", where the "else" part can contain another "if-else". – Some programmer dude Jan 15 '17 at 06:40
  • The above comments answer your question AFAIK. – Tim Biegeleisen Jan 15 '17 at 06:41
  • 1
    And as said by @DYZ the two snippets you show are not equivalent. And what's possibly worse is that the second snippet doesn't follow the rules you set up in the comments in the first snippet. – Some programmer dude Jan 15 '17 at 06:42
  • Please view my edit. I understand that they are completely different - one ensures mutual exclusivity and the other does not. I just am wondering why else-if isn't defined such that it runs EXACTLY like code snippet 2. My second code snippet is an equivalent way of writing what I THINK snippet 1 should be naturally equivalent to. – rb612 Jan 15 '17 at 06:44
  • What do you mean "isn't defined"? It is, or else your snippet #2 won't be valid, but it is. – DYZ Jan 15 '17 at 06:46
  • Okay, let me try to reword this. I have these two statements that run differently depending on if the conditions are mutually exclusive. I think it would make sense to have else-if work so that once condition A fails, it checks condition B,C, and D, just like snippet 2. However, it does not do this, and I'm wondering if it's because of convenience (to stop nesting) or something else. – rb612 Jan 15 '17 at 06:48

3 Answers3

2

The reason one would prefer one style over the other is to ensure either the presence or lack of mutual exclusion when testing the conditions.

If it is the case that Condition B, C, or D are not mutually exclusive with one another, then...

  • ...in the first scenario, only Condition B would fire, due to the mutual exclusivity of the else if statement.
  • ...in the second scenario, Conditions B, C, and D would fire, due to the fact that they are not mutually exclusive due to the if statement.

Ultimately it depends on what you want to do. You may want to run multiple statements in this fashion. However, you probably don't. Fashioning your statements in a mutually exclusive way ensures that you don't run into strange logical bugs when you get a result or state that you didn't expect.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • This helps, thank you! So instead of nesting, this is a more elegant way to force mutual exclusivity in essence? – rb612 Jan 15 '17 at 06:50
  • @rb612: I'd almost argue that a `switch` with either a `return` or `break` (depending on what you're doing with the result) would be more help to clarify that in many cases. But again, it really does depend on what you want to or have to do with the logic in this scenario. I've seen in professional and hobby development that you *generally* want mutually exclusive events (hence the first pattern), but the second pattern has its place. – Makoto Jan 15 '17 at 06:52
2

If you take your nesting approach, and apply it consistently, you would actually come up with this:

if (condition A) {
    // A
} else {
    if (condition B) {
        // B
    } else {
        if (condition C) {
            // C
        } else {
            if (condition D) {
                // D
            }
        }
    }
}

Each if gets treated the same way. The first if statement doesn't have any special ability to remove the else block from all the other if statements. The grammar you suggest gives else an inconsistent meaning.

4castle
  • 32,613
  • 11
  • 69
  • 106
  • Okay yes I think you're getting to the essence of my question - "else" usually means if the above statement is false, execute, but in the case of elifs, it is saying, "if ALL the above statements are false, execute". – rb612 Jan 15 '17 at 06:55
0

Let's take a basic example.

Assume, You want to award a grade according to the score of the student and score is 60.

if(score <= 50) {
   System.out.println("C Grade");
} else if(score <= 70) {
   System.out.println("B Grade");
} else if(score <= 100) {
   System.out.println("A Grade");
}

It will print B Grade.

if(score<=50) {
   System.out.println("C Grade");
} else {
    if(score <= 70) {
        System.out.println("B Grade");
    }
    if(score <= 100) {
        System.out.println("A Grade");
    }
}

Now, According to you if we follow above approach where conditions are not mutually exclusive. It will print B Grade and A Grade which is not true.

So, in the cases where conditions in if are not mutually exclusive you will run into problems. That's why nesting of if..else is needed.

Hardik Modha
  • 12,098
  • 3
  • 36
  • 40
  • Definitely, thank you for your answer. I do understand that they are different, I was just wondering why the first example doesn't run exactly like the second example (due to the way the language treats else-if) – rb612 Jan 15 '17 at 06:52
  • Thanks for your answer and edit. Please see my edit to the question, as I think it will help clarify what exactly I don't understand. – rb612 Jan 15 '17 at 06:59
  • Yeah, What you finally understood is correct. `else` only check that above condition is not met and executes, but `else..if` checks that above all conditions are not met. :) – Hardik Modha Jan 15 '17 at 07:03