Mega Code Archive

 
Categories / C Tutorial / Statement
 

Use switch to simplify the logic

#include <stdio.h> int main(void) {   int choice = 2;                    if((choice>10) || (choice <1)){     choice = 11;                    }      switch(choice)   {     case 7:       printf("\nCongratulations!");       break;                         case 2:       printf("\nA");       break;                           case 8:       printf("\nB");       break;          case 11:       printf("\nC");     default:       printf("\nSorry, you lose.\n");       break;                }   return 0; } A