//* some variations on IF and SWITCH *

int ifLeapYear(int y) { return ( y%4==0 && ( y%100!=0 || y%400==0 ) ); } int getMonthDays1 (int m, int y) { int days; if (m==1) days=31; else if (m==2) days=28+ifLeapYear(y); else if (m==3) days=31; else if (m==4) days=30; else if (m==5) days=31; else if (m==6) days=30; else if (m==7) days=31; else if (m==8) days=31; else if (m==9) days=30; else if (m==10) days=31; else if (m==11) days=30; else if (m==12) days=31; else days=-1; // it is important to check for each month value and return "illegal" // number of days for illegal month number or signal the error in some // other way. Another good way is print an error message. // it is also important to use if-else to prevent further checking // of the value of m when one already was found and days set to correct value return(days); } int getMonthDays2 (int m, int y) { if (m==1) return(31); if (m==2) return(28+ifLeapYear(y)); if (m==3) return(31); if (m==4) return(30); if (m==5) return(31); if (m==6) return(30); if (m==7) return(31); if (m==8) return(31); if (m==9) return(30); if (m==10) return(31); if (m==11) return(30); if (m==12) return(31); return(-1); // if you use return you do not have to use else because further tests // would not be performed anyway. return exits the procedure } int getMonthDays3 (int m, int y) { int days; if (m==1||m==3||m==5||m==7||m==8||m==10||m==12) days=31; else if (m==4||m==6||m==9||m==11) days=30; else if (m==2) days=28+ifLeapYear(y); else days=1; // you can simplity the function by using more complex IF condition return(days); } int getMonthDays4 (int m, int y) { int days; switch (m) { case 1: days=31; break; case 2: days=28+ifLeapYear(y); break; case 3: days=31; break; case 4: days=30; break; case 5: days=31; break; case 6: days=30; break; case 7: days=31; break; case 8: days=31; break; case 9: days=30; break; case 10: days=31; break; case 11: days=30; break; case 12: days=31; break; default: days=-1; } // switch construction is perfect for this kind of multiple if-else // it looks well organized and can be easily checked for correctness return(days); } int getMonthDays5 (int m, int y) { int days; switch (m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days=31; break; case 2: days=28+ifLeapYear(y); break; case 4: case 6: case 9: case 11: days=30; break; default: days=-1; } // switch can be optimized so taht you do not have to repeat multiple // instructions, just reorganize cases. Make sure that the default case // is always the very last one (just in case the compiler is not smart // enough to reorganize the list of values on its own) return(days); } int getMonthDays6 (int m, int y) { switch (m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return(31); case 2: return(28+ifLeapYear(y)); case 4: case 6: case 9: case 11: return(30); default: return(-1); } // you can use immediate return to avoid using break } int getMonthDays7 (int m, int y) { const char mdays[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // a look-up table is faster than several if-else statements and should be used // if values to pick from are integer and are in ranges // Note that we chose to start the array at [1] to avoid using slower [m-1] int days = -1; if (m<0 || m>12) days=-1; else if (m==2) days=mdays[m]+ifLeapYear(y); else days=mdays[m]; return(days); }