-2

What's the point in making an else if statement when you could just make another if statement? i.e

With an else statement:

if (x > 1) {
    System.out.println("Hello!");
}else if (x < 1) {
    System.out.println("Bye!");
}

Without an else statement:

if (x > 1) {
    System.out.println("Hello!");
}
if (x < 1) {
    System.out.println("Bye!");
}

Both codes give out the same solution, so what's the point typing 'else' when the output is no difference?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Tyler
  • 191
  • 17
  • 8
    Change the condition of the second one to `x > 2`, and pass in `x = 3` and see the magical difference. – Kayaman Jan 09 '18 at 10:59
  • 2
    the difference is: in the first example, the second statement/condition won't be evaluated, int he second it will. a bit more efficiënt – Stultuske Jan 09 '18 at 11:00
  • 1
    This is a completely different question, because the duplicate uses `return` in each branch. In the duplicate there is no difference, and `else` is unnecessary. Here, there is a significant difference. Voting to re-open. – Sergey Kalinichenko Jan 09 '18 at 11:12
  • Edited the statement, I just want to know the difference between else if, and if. I know they are mutually conclusive now, it was just a quick question Didn't see the mistake. – Tyler Jan 09 '18 at 11:19
  • The edit obsoleted the answers you received by making the conditions *not* mutually-exclusive. I've rolled it back. – T.J. Crowder Jan 11 '18 at 11:23
  • That's why I got random downvoted though... – Tyler Jan 11 '18 at 11:26

4 Answers4

2

The big difference is that with the else, the second condition will not be checked at all if the first condition was true. Without the else, the second condition will be checked.

Ramifications of that:

  • If the conditions are not mutually-exclusive, without the else both could match and both if bodies could be executed.
  • If the conditions involve side effects, without the else the second evaluation will trigger the side-effect a second time.
  • As dasblinkenlight pointed out in a now-deleted comment below (and now in his answer), if x is a volatile field and its value is changed by another thread after the first check and before the second, without the else both conditions could match and both if bodies could be executed.

To see what I mean about evaluation, let's make x a function call with a side effect instead of just a variable (Java and JavaScript are very different, but in this case, we can use JavaScript to demonstrate the concept):

function x() {
  console.log("x called");
  return 42;
}

console.log("With else:");
if (x() > 1) {
  console.log("> 1");
} else if (x() < 1) {
  console.log("< 1");
}

console.log("Without else:");
if (x() > 1) {
  console.log("> 1");
}
if (x() < 1) {
  console.log("< 1");
}
.as-console-wrapper {
  max-height: 100% !important;
}

Note that x is called twice without the else, but only once with the else.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

In your example it makes no difference but now if you have something a little bit different like this:

if (x >= 1) {
    System.out.println("Hello!");
}
if (x <= 1) {
    System.out.println("Bye!");
}

It will print the two string (if x equal 1) as the conditions are both true

Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63
1

As far as the logic is concerned, as long as the two statements use mutually exclusive condition, and it is not possible to change x concurrently with your running thread, there would be no difference.

However,

  • There would be a difference in execution if x is modified concurrently: the second version could produce both "Hello" and "Bye", or no output at all
  • There would be a difference in execution because the second condition must be re-evaluated in your second snippet regardless of the computation of the first condition, while your first snippet will evaluate only one condition when x > 1
  • The above assumes that the statements inside the first condition do not interrupt the thread, i.e. it's not a return, a break, a continue, or a throw. If the thread is interrupted, else is no longer necessary.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

The difference is on the runtime. In the if...else if... case if the case of if it is correct the else if will not be examined. In if.. and then another if.. both the cases of if's will be examined, so on the runtime you have one more comparison. In such a small programm will not make the difference, but consider a huge program with thousands of if cases. Hope I covered you!

Nwlis
  • 63
  • 6