[Solved] Why do we structure else-if as such? [duplicate]


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.

2

solved Why do we structure else-if as such? [duplicate]