In cases like these, you have a simple map where an integer in a not too long, contiguous sequence goes in (the month) and a number comes out (the number of days).
You can do this with several IF's, or using the OR syntax as has been suggested.
if ((month == 1) || (month == 3) || ... )
The above would be the best way if you had very few group of very few items (especially if noncontiguous) and very few possible outputs. The second condition is true (you only have 28/29, 30 and 31), the first... just barely, in my opinion.
In general, I find it safer to do this with a vector:
// Vectors start at #0
int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// Correct data later
if (leapyear) {
days[2]++;
}
if (day > days[month]) {
printf("Month %d has only %d days\n", month, days[month]);
}
If you had a fixed number of noncontiguous items, you could do this with two vectors. This works with any grouping, unless you have really many
keys or can come up with an easy rule (of course, if you wanted to check
which day of the week a random even day was, you would not enter all 182
or 183 even-numbered days; you'd check it was even and use modulo 7).
// I check all months except in summer.
// Keys holds the "question" (which month?), values the answer.
int keys = { 1, 2, 3, 4, 5 , 9, 10, 11, 12 };
int values = { 31, 28, 31, 30, 31, 30, 31, 30, 12 };
int i;
// I use 0 to indicate that the check does not apply (e.g. August)
days = 0;
for (i = 0; i < 12; i++) {
if (month == keys[i]) {
days = values[i];
}
}
if (days != 0) {
if (day > days) {
printf("Month %d has only %d days\n", month, days);
}
}
A fancier way of doing this (for months only) could be the knuckle mnemonic
// 1st hand knuckle february leap
int days = 31-((month<8)^(month%2))-(2==month)*(month-leapyear);
if (day > days) {
printf("Month %d has only %d days\n", month, days);
}