How to use switch

Match a value

A switch compares an integer-like expression with constant case labels.

switch (choice) {
case 1:
    printf("Add\n");
    break;
case 2:
    printf("Remove\n");
    break;
default:
    printf("Unknown choice\n");
    break;
}

End cases deliberately

Without break, execution continues into the next case. Use fallthrough only intentionally and mark it with a clear comment.

Group labels

Several labels can share one body when they should behave alike.

switch (letter) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
    printf("vowel\n");
    break;
default:
    printf("not a lowercase vowel\n");
}

Know its limits

C switch does not match strings, ranges, or floating-point values. Use if/else or string comparison for those cases.