Skip to content
StudyFame

switch Statement in C

When you choose an option from a menu, like 1 for Add, 2 for Subtract, 3 for Multiply, the program has to pick one action from many fixed values. You could use a long else-if ladder, but C has a cleaner tool: the switch statement.

In this lesson you will learn switch, its rules, why break is so important, and build a menu-driven calculator, a top exam program.

What You Will Learn

  • The syntax of switch, case, break and default.
  • The rules of switch (what is and isn't allowed).
  • What fall-through is and how break stops it.
  • A menu-driven calculator program.
  • Grouping cases (vowel check).

switch Statement in C Explained

Exam definition: The switch statement is a multi-way decision statement that compares the value of an expression with several constant case labels and runs the code of the matching case. break ends the switch, and default runs when no case matches.

Memory trick: switch is like a lift (elevator). You press a floor number (the expression); the lift opens at the matching floor (case). break = get out. Forget break and you keep going down to the next floors (fall-through). default = the ground floor if your number doesn't exist.

Syntax of switch case in C

switch (expression)
{
    case value1:
        /* statements */
        break;
    case value2:
        /* statements */
        break;
    default:
        /* runs if no case matches (optional) */
}

Rules of switch

  • The expression must give an integer or a character (int, char, enum). Not float, double or string.
  • Case labels must be constants: case 1:, case 'A':. Not variables, not ranges like case x > 5:.
  • Two cases cannot have the same value.
  • default is optional and can be placed anywhere (usually at the end).
  • Without break, execution falls through into the next case.

Example 1: Menu Driven Calculator Using switch case

#include <stdio.h>

int main(void)
{
    int choice;
    float a, b;

    printf("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    printf("Enter two numbers: ");
    scanf("%f %f", &a, &b);

    switch (choice)
    {
        case 1:
            printf("Result = %.2f\n", a + b);
            break;
        case 2:
            printf("Result = %.2f\n", a - b);
            break;
        case 3:
            printf("Result = %.2f\n", a * b);
            break;
        case 4:
            if (b != 0)
                printf("Result = %.2f\n", a / b);
            else
                printf("Cannot divide by zero\n");
            break;
        default:
            printf("Invalid choice\n");
    }

    return 0;
}

Output

1. Add
2. Subtract
3. Multiply
4. Divide
Enter your choice: 3
Enter two numbers: 12 5
Result = 60.00

Code Explanation

  1. The menu is printed and the user chooses 3.
  2. switch (choice): C compares 3 with each case label.
  3. case 3: matches, so it prints 12 × 5 = 60.00.
  4. break; exits the switch immediately; cases 4 and default are skipped.
  5. If the user enters 9, no case matches, so default prints "Invalid choice".

How switch Works

  1. The expression is calculated once.
  2. C jumps directly to the matching case label (compilers often use a fast jump table).
  3. From there it runs statements downwards until it meets break or the end of the switch.
  4. If there is no break, it continues into the next case's statements: this is fall-through.
  5. If no case matches and there is a default, it jumps there; otherwise it does nothing.

Example 2: Grouping Cases with Fall-Through (Vowel Check)

#include <stdio.h>

int main(void)
{
    char ch;

    printf("Enter a letter: ");
    scanf(" %c", &ch);

    switch (ch)
    {
        case 'a': case 'e': case 'i': case 'o': case 'u':
        case 'A': case 'E': case 'I': case 'O': case 'U':
            printf("%c is a vowel\n", ch);
            break;
        default:
            printf("%c is a consonant\n", ch);
    }

    return 0;
}

Output

Enter a letter: E
E is a vowel

Empty cases fall through to the next one, so all ten vowel labels share one block. Here fall-through is useful. When it happens by mistake (a forgotten break), it is a bug.

Important Points to Remember

  • switch works only with int and char values (not float or strings).
  • Case labels must be unique constants.
  • break exits the switch; without it, execution falls through.
  • default is optional and runs when no case matches.
  • Best for menus and choosing from fixed values.

Common Mistakes Beginners Make

MistakeWhat happensFix
Forgetting breakThe next cases also runEnd each case with break;
switch (price) with a floatCompile errorUse if-else for decimals
case x: with a variableError: case label is not a constantUse constants
case marks >= 90:Error / not a range checkUse an else-if ladder for ranges
case 1; (semicolon)Syntax errorUse a colon: case 1:

switch case Practice Questions

  1. What is a switch statement? Write its syntax.
  2. What is the use of break in switch?
  3. What is fall-through? Is it always a mistake?
  4. Can we use a float in a switch expression?
  5. Is default compulsory? Where can it be placed?
Show answers
  1. A multi-way decision statement that runs the case matching the expression's value. switch (expr) { case v1: ... break; default: ... }
  2. It exits the switch so the following cases do not run.
  3. When execution continues into the next case because there is no break. It is useful for grouping cases, but a bug when done by mistake.
  4. No, only integer types and characters.
  5. No, it is optional, and it can be placed anywhere inside the switch.

Coding Exercise

Read a day number (1–7) and print the day name (1 = Monday ... 7 = Sunday). Print "Invalid day" for any other number.

Show solution
#include <stdio.h>

int main(void)
{
    int day;

    printf("Enter day number: ");
    scanf("%d", &day);

    switch (day)
    {
        case 1: printf("Monday\n");    break;
        case 2: printf("Tuesday\n");   break;
        case 3: printf("Wednesday\n"); break;
        case 4: printf("Thursday\n");  break;
        case 5: printf("Friday\n");    break;
        case 6: printf("Saturday\n");  break;
        case 7: printf("Sunday\n");    break;
        default: printf("Invalid day\n");
    }
    return 0;
}
Enter day number: 5
Friday

Quick Quiz: switch case in C (MCQ)

  1. Which type is NOT allowed in a switch expression?

    A) int   B) char   C) float   D) enum

  2. What stops fall-through?

    A) continue   B) break   C) default   D) return 0 only

  3. Output of: int x = 2; switch (x) { case 1: printf("A"); case 2: printf("B"); case 3: printf("C"); }

    A) B   B) BC   C) ABC   D) C

  4. default runs when:

    A) Always   B) The first case matches   C) No case matches   D) Never

  5. Which is a valid case label?

    A) case x:   B) case 'A':   C) case 1.5:   D) case "yes":

Show answers
  1. C: float.
  2. B: break.
  3. B: BC (no breaks, so it falls through from case 2).
  4. C: when no case matches.
  5. B: case 'A':

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

switchMulti-way decision on one value
Allowed typesint, char, enum (not float, not string)
Case labelsUnique constants
breakExits the switch
No breakFall-through to the next case
defaultOptional; runs when no case matches

Frequently asked questions

Why is break used in switch?
To exit the switch after a case; without it, execution falls through to the next case.
Can switch work with strings?
No. Only integer and character values; use if-else with strcmp() for strings.
Can two cases have the same value?
No, it is a compile error: duplicate case value.
Is default compulsory?
No, it is optional, but good for handling invalid input.

Remember

  • switch = lift: break = get out, default = ground floor.
  • Constants only, no ranges, no floats.

Next Topic

Next lesson: switch vs if-else in C. You will compare switch with if-else and learn when to use which.