Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SWITCH-STATEMENT EXAMPLE - C
- // (c) gigabytemon
- // 17/06/2020
- // 2862 abc5 66cb a008 dbd9 5bf7 0dda 04f8
- // e192 33e4 e689 4efb 24b9 d34f 6c2d 1ee1
- /* introduction
- * ------------
- * switch statements are switches that take an input and perform a corresponding, pre-programmed output.
- * this input is called the SWITCH CASE.
- * this neat little statement lets you test an input to see if it is equal to a list of potential values.
- * these values are called CASES.
- * 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.
- */
- /* syntax
- * ------
- * the syntax of a switch statement goes like this:
- *
- * int INPUT; | char INPUT;
- * switch (INPUT) { | switch (INPUT) {
- * case 1: | case 'a':
- * statements; | statements;
- * break; | break;
- * case 2: | case 'b':
- * statements; or statements;
- * break; | break;
- * case 3: | case 'c':
- * statements; | statements;
- * break; | break;
- * . | .
- * . | .
- * . | .
- * default: | default:
- * statements; | statements;
- * break; | break;
- * } }
- */
- /* personal notes
- * --------------
- * switch statements are excellent for menues, because you can clearly list and differentiate between expected inputs and outputs.
- * switches are basically advanced if-else comparators, where each if-else comparison can be simplified to a case.
- * 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
- */
- #include <stdio.h>
- int main()
- {
- int input;
- //print menu
- printf("MENU\n");
- printf("1. Print out \"what's up, nibba\"\n");
- printf("2. Add 2 + 2\n");
- printf("3. Print ERROR 418\n");
- printf("0. EXIT\n");
- //loop until 0 or any number not on the menu is input
- do {
- printf("Please pick an option: ");
- scanf("%d", &input);
- switch(input) {
- case 1:
- printf("what's up, nibba\n");
- break;
- case 2:
- int sum = 2 + 2;
- printf("2 + 2 = %d\n", sum);
- break;
- case 3:
- printf("I am a teapot.\n");
- break;
- default:
- break;
- }
- } while (input != 0);
- printf("Thank you for using this menu.\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment