Thuta Learning
BasicProgrammingbeginner

Switch

Relax. We'll talk through this in plain words — no textbook voice.

switch is used when you want to compare one value against several cases. For things like menu options, day numbers, or status codes, if...else it can be cleaner than writing a long chain of these.

c
#include <stdio.h>

int main() {
  int day = 4;

  switch (day) {
    case 1:
      printf("Monday");
      break;
    case 4:
      printf("Thursday");
      break;
    default:
      printf("Unknown day");
  }
  return 0;
}

day's value is 4, so case 4's code runs. break then exits out of the switch.

You should see
Thursday

Info

default is the fallback that runs when nothing matches any case. It comes in handy when you're handling user input.

Easy traps

  • If you forget to add break, execution keeps running into the next cases too. This is called fall-through, and unless you're doing it on purpose, it usually turns into a bug.
Switch | Thuta Learning