c - Using if statement instead of default of switch case -
is correct use if statement before opening switch
case
, avoid using default
keyword? example want program takes input number of month , tells name. code uses switch
case
statement:
#include <stdio.h> #include <stdlib.h> main() { int month; printf("insert number of month , program return name"); scanf("%i", &month); switch (month) { case (1): printf("the month january"); break; case (2): printf("the month february"); break; case (3): printf("the month march"); break; case (4): printf("the month april"); break; case (5): printf("the month may"); break; case (6): printf("the month june"); break; case (7): printf("the month july"); break; case (8): printf("the month august"); break; case (9): printf("the month september"); break; case (10): printf("the month october"); break; case (11): printf("the month november"); break; case (12): printf("the month december"); break; default: printf("not valid"); } system("pause"); return 0; }
then, wondering if can put non-validity condition in if
statement rather in default
keyword. me seems correct since want verify value before program executes switch
case
statement. think, correct? if i'm not asking much, please tell me why?
the code if
statement:
#include <stdio.h> #include <stdlib.h> main() { int month; printf("insert number of month , program return name"); scanf("%i", &month); if (month >= 1 && month <= 12) { switch (month) { case (1): printf("the month january"); break; case (2): printf("the month february"); break; case (3): printf("the month march"); break; case (4): printf("the month april"); break; case (5): printf("the month may"); break; case (6): printf("the month june"); break; case (7): printf("the month july"); break; case (8): printf("the month august"); break; case (9): printf("the month september"); break; case (10): printf("the month october"); break; case (11): printf("the month november"); break; case (12): printf("the month december"); break; default:; } } else { printf("not valid"); } system("pause"); return 0; }
thank , sorry english not mother tongue. let me know if haven't explained myself clearly.
both approaches valid , equivalent except few details:
- the way define
main
obsolete. not compile c99 compiler in strict mode. useint main(void)
orint main(int argc, char *argv[])
. - test return value of
scanf()
. if type cannot parsed number,scanf()
return0
oreof
if close input stream, , rest of code use uninitialized value ofmonth
. - it neither useful nor idiomatic parenthesize value in
case
clauses. remove them. - it safer add
break;
statement @ end of last clause inswitch
statement,default
or not. if ever add clause, wont risk omitting it. - remove
default:;
clause second code, useless , surprising.
the reason why second approach might more indicated in case want different if input out of range, such restarting input operation. if
statement allow separate these situations correctly, whereas using default
clause might not appropriate:
for (;;) { int n, month; printf("enter number between 1 , 12: "); n = scanf("%d", &month); if (n == eof) { printf("unexpected end of file\n"); exit(1); } if (n != 1) { printf("invalid input\n"); scanf("%*[^\n]%*c"); /* flush pending input */ continue; } if (month >= 1 && month <= 12) { switch (month) { case 1: printf("the month january\n"); break; ... case 12: printf("the month december\n"); break; } handle_month(month); // perform other tasks break; } else { printf("invalid month number\n"); } }
Comments
Post a Comment