2

So I have a question that I now have had for a long time, what are the differences between using else if and chained if statements, don't they produce the same thing? For example take this basic example:


short age {0};
std::cin >> age;

if(age > 18) cout << "You are an adult";
if(age < 18) cout << "YOu are a teen";
if(age == 0) cout << "YOu are born";

and


short age {0};
std::cin >> age;

if(age > 18) cout << "You are an adult";
else if(age < 18) cout << "YOu are a teen";
else if(age == 0) cout << "YOu are born";

How do they differ?

Alex
  • 840
  • 7
  • 23
  • 2
    Related (maybe dupe): [Performance difference of “if if” vs “if else if”](https://stackoverflow.com/q/7970275/2602718) – scohe001 Mar 13 '20 at 15:15
  • Assuming the compiler doesn't optimize it away, the first executes an additional test that isn't needed when age is greater than 18. – L. Scott Johnson Mar 13 '20 at 15:15
  • @Fran I realized that's for return's, which is a little different (which is why I retracted my vote). I think this may be a better dupe target (despite being Java): https://stackoverflow.com/q/20259351/2602718 – scohe001 Mar 13 '20 at 15:18
  • @scohe001 Reopened. – François Andrieux Mar 13 '20 at 15:20
  • Oh I meant add the second link, @Fran. I agree this question shouldn't stay open (though I can't vote after retracting) – scohe001 Mar 13 '20 at 15:24
  • In what you called `chained if statements` every `if` will be checked, in `else if` variation once one `if` is statisfied all next `elses` will be skipped. – muaz Mar 13 '20 at 15:29

2 Answers2

5

In your specific example they are the same (note: question edited; now they are not). Anyway consider something like:

Example 1: Modifying variables

if (age > 18) age = 17; // executes
if (age < 18) ... // executes!

Now you can see this would behave differently than

age = 19;
if (age > 18) age = 17; // executes
else if (age < 18) ... // skipped!

Example 2: Overlapping conditions

Or as @cubic mentions, if the conditions overlap, there will also be a difference.

age = 20;
if (age > 19) ...; // executes
if (age > 17) ...; // executes!

versus:

age = 20;
if (age > 19) ...; // executes
else if (age > 17) ...; // skipped!
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
tenfour
  • 36,141
  • 15
  • 83
  • 142
  • Not that the body of the first `if` has to modify anything, the conditions may overlap to begin with. – Cubic Mar 13 '20 at 15:16
3

if you use the chained if statements. the all if statement will be executed. but if you use if else-if then it will execute only the true statement

e.g.

age = 10
if(age > 18) cout << "you are an adult"
if(age < 18 && age > 11) cout << "your are not an adult"
if(age < 11 ) cout << "you are a child"

in this case all three if statements will run

but in other case

age = 12
if(age > 18) cout << "you are an adult"
else if(age < 18 && age > 10) cout << "your are not an adult"
else if(age < 10 ) cout << "you are a child"

only first if will check and then the second else if(age < 18 && age > 10) it will not check the last condition because it found the condition he want.