Tutorials

C Tutorial

All Tutorials Learning Tools Login

C Tutorial · Chapter 11 of 16

C Switch

Choose from fixed menu options.

switch is useful when the user picks from numbered choices.

Remember break in C so execution does not fall through to the next case.

Switch sample Try it Yourself »
#include <stdio.h>

int main(void) {
    int choice;
    printf("1. Add\n2. Subtract\nChoose: ");
    scanf("%d", &choice);
    switch (choice) {
        case 1: printf("Addition selected\n"); break;
        case 2: printf("Subtraction selected\n"); break;
        default: printf("Invalid choice\n");
    }
    return 0;
}

Classroom Tips

  • Always include a default case for invalid input.