the program is meant to output 31 days
why does it output 30 days?
what have i done wrong

Code:
```int main()
{
int month, days;

month = 7;

if (month == 2) {
days = 28;
}
else if (month == 4 || 6 || 9 || 11 ) {
days = 30;
}
else {
days = 31;

}

printf( "days=%d\n", days );

return 0;
}``` 2. This
Code:
`else if (month == 4 || 6 || 9 || 11 ) {`
Should be
Code:
`else if (month == 4 || month == 6 || month == 9 || month == 11 ) {`
You would probably be better off with a switch statement, more 3. hmm
why wont ( month == 4 || 6 || 9 || 11 ) work?
is this statement incomplete if its written like this? 4. Yea that isn't valid.

EDIT - If you want to know why its because of the way it is evaluated, its called Short Circuit Evaluation, you can google for it if your really interested. But basically it stops evaluating operands of logical operation once the result is known. 5. No, the reason it outputs 30 is because if(6) is always true. The first expression, month==4, evaluates to false. After the first expression is evaluated to false the OR is evaluated and the next expression must be evaluated. Since 6 is a non-zero value the expression evaluates to true. Because only one of the expressions must evaluate true, the if evaluates true and the code block is executed. The if statement is valid, it just doesn't contain the correct logic for what you are trying to do. Remember: in C if(<zero>) == true, if(<any non-zero value>) == false. 6. ## ~

Here's the code using a switch statement, which is prettier:

Code:
```switch (month)
{
case 2: days = 28; break;
case 4:
case 6:
case 9:
case 11: days = 30; break;
default: days = 31; break;
}``` Popular pages Recent additions 