Else if problems! Noob question!

I'm creating a simple program which asks the user to input their age, and it should give different outputs, depending on what the user has entered.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Age Ting - LIAM
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main (void)
{
    int age;
    printf("Please enter your age here: ");
    scanf("%d",&age);
    if (age < 5) printf("\n\nToo young for school");
    else if (age < 16) printf("\n\nStill at school");
    else if (age > 15) printf("\n\nGo to work!");
    else if (age > 59) printf("\n\nTime to retire.");
    getch();
}
    


The following three work correctly:

Too young for school
Still at school
Go to work!

But the Time to retire one doesn't kick in. Whenever an age which is over 59 is entered, it still shows as "Go to work!".

Any ideas?
That's because a number greater than 59 happens to be greater than 15.
What you posted is C, by the way.
Last edited on
Ok.. so how do I fix it?

And also.. isn't a number which is smaller than 5, also smaller than 16? So should they not both display the same message to?

:)
No, because the check for <5 happens before the one for <16.
The order of these statements matters, but that should be kind of obvious...
just go through the program yourself step by step.
Last edited on
Else-if statements are exclusive: if you go into one, you skip all the other ones.

So if it goes into age > 15, it won't go into age > 59. Just switch those two around and you'll be fine. Either that or make it age > 15 AND age <59
closed account (S6k9GNh0)
>.> In lamens terms ffs

You have four condition if statements. This says, if this condition is met, do this, else, if this other condition is met, do this, else if this condition is met, do this, else, if this condition is met, do this.

Another way you can try this is to make the conditions check to see if the an age is under the requirements only so once the first condition is met, the others won't be executed.

1
2
3
4
    if (age < 5) printf("\n\nToo young for school");
    else if (age <= 16) printf("\n\nStill at school");
    else if (age <= 59) printf("\n\nGo to work!");
    else if (age > 59) printf("\n\nTime to retire."); //This is an exception since you don't have any conditions after this. 


This is really simple code. Try not to over-complicate it too much as it is, after all, self explanatory. The if statements require a condition to be met in order to execute its body and an else if statement requires the previous condition to fail in order for its condition statement to be tried.

The conditions themselves are just comparisons, commonly used in math (or similar to). >= means greater than or equal too, <= means less than or equal too.

Or did I miss the point...
Last edited on
The most straight forward and less error prone way is probably to specify your bounds explicitly e.g
1
2
3
if(age >=0 && age < 5) printf("\n\nToo young for school");
else if(age >=5 && age < 15) printf("\n\nStill at school");
...


I personally prefer cascade along the cases, kinda like what you did but without changing the comparison operator cuz that typically causes problems for me. I would write it as

1
2
3
4
5
scanf("%d",&age);
if (age < 5)        printf("\n\nToo young for school");
else if (age < 16)  printf("\n\nStill at school");
else if (age < 60)  printf("\n\nGo to work!");
else                printf("\n\nTime to retire.");
Last edited on
spaggy wrote:
The most straight forward and less error prone way is probably to specify your bounds explicitly


I disagree with that example, because:

- it has redundant code. redundant code = more error prone, higher maintanance.

- it doesn't exclude conditions with else, so it's less efficitient because you're checking the same thing over and over again.

- && and || operators in large conditionals are often a source of confusion. People tend to mix them up and put the wrong one. And it's not just newbies. Sometimes when I use them I have to think twice about exactly which I'm going for. So even more error prone.


I would write it as <code>


That's how I would write it, too. IMO, that's also the most straightforward and least error prone way (which is why I prefer it).
Last edited on
Topic archived. No new replies allowed.