gigabytemon

C Examples: Switch Statement

Jun 17th, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. // SWITCH-STATEMENT EXAMPLE - C
  2. // (c) gigabytemon
  3. // 17/06/2020
  4. // 2862 abc5 66cb a008 dbd9 5bf7 0dda 04f8
  5. // e192 33e4 e689 4efb 24b9 d34f 6c2d 1ee1
  6.  
  7. /* introduction
  8.  * ------------
  9.  * switch statements are switches that take an input and perform a corresponding, pre-programmed output.
  10.  * this input is called the SWITCH CASE.
  11.  * this neat little statement lets you test an input to see if it is equal to a list of potential values.
  12.  * these values are called CASES.
  13.  * when the SWITCH CASE is found to be equal to one of the CASES, the statement will run its corresponding code as written in the CASE.
  14.  */
  15.  
  16. /* syntax
  17.  * ------
  18.  * the syntax of a switch statement goes like this:
  19.  *
  20.  * int INPUT;               |       char INPUT;
  21.  * switch (INPUT) {         |       switch (INPUT) {
  22.  *      case 1:             |           case 'a':
  23.  *          statements;     |               statements;
  24.  *          break;          |               break;
  25.  *      case 2:             |           case 'b':
  26.  *          statements;     or              statements;
  27.  *          break;          |               break;
  28.  *      case 3:             |           case 'c':
  29.  *          statements;     |               statements;
  30.  *          break;          |               break;
  31.  *          .               |               .
  32.  *          .               |               .
  33.  *          .               |               .
  34.  *      default:            |           default:
  35.  *          statements;     |               statements;
  36.  *          break;          |               break;
  37.  * }                                }
  38.  */
  39.  
  40. /* personal notes
  41.  * --------------
  42.  * switch statements are excellent for menues, because you can clearly list and differentiate between expected inputs and outputs.
  43.  * switches are basically advanced if-else comparators, where each if-else comparison can be simplified to a case.
  44.  * p.s. yes, as with if-else, you can put a switch inside a switch, BUT DON'T DO THIS UNLESS YOU WANT TO LOOK LIKE A MONKEY DURING YOUR DEBUG PHASE
  45.  */
  46.  
  47. #include <stdio.h>
  48. int main()
  49. {
  50.     int input;
  51.    
  52.     //print menu
  53.     printf("MENU\n");
  54.     printf("1. Print out \"what's up, nibba\"\n");
  55.     printf("2. Add 2 + 2\n");
  56.     printf("3. Print ERROR 418\n");
  57.     printf("0. EXIT\n");
  58.    
  59.     //loop until 0 or any number not on the menu is input
  60.     do {
  61.         printf("Please pick an option: ");
  62.         scanf("%d", &input);
  63.        
  64.         switch(input) {
  65.             case 1:
  66.                 printf("what's up, nibba\n");
  67.                 break;
  68.            
  69.             case 2:
  70.                 int sum = 2 + 2;
  71.                 printf("2 + 2 = %d\n", sum);
  72.                 break;
  73.                
  74.             case 3:
  75.                 printf("I am a teapot.\n");
  76.                 break;
  77.                
  78.             default:
  79.                 break;
  80.         }
  81.     } while (input != 0);
  82.    
  83.     printf("Thank you for using this menu.\n");
  84.    
  85.    
  86. }
Advertisement
Add Comment
Please, Sign In to add comment