Switch statement in C in C Language

Updated on May 31, 2025 | By Learnzy Academy

The switch statement in C is a powerful control structure used to handle multiple conditions based on the value of a single variable or expression, typically of integer or character type. It provides a cleaner and more readable alternative to lengthy if-else chains when dealing with a fixed set of values. The switch statement evaluates the expression once and then compares its result against various case labels. When a matching case is found, the corresponding block of code is executed. 

How it works:

  1. The switch evaluates the expression inside the parentheses.
  2. It then compares the result to the values in each case.
  3. When a match is found, the corresponding block of code is executed.
  4. The break statement exits the switch block after executing a case.
  5. If no match is found, the default block (if present) is executed.

Syntax of Switch-

switch (expression) {
    case constant1:
        // code block
        break;
    case constant2:
        // code block
        break;
    ...
    default:
        // default code block
}

 

Click here to download practice questions on Switch statement in C