🟢 Beginner  ·  Lesson 12

switch-case Statement

switch-case Statement

switch Statement Syntax

The switch statement is a multi-way decision statement. It tests a variable against a list of constant values (cases) and executes the matching block.

Syntax
switch (expression) {
    case value1:
        // code for case 1
        break;
    case value2:
        // code for case 2
        break;
    default:
        // code if no case matches
}
  • expression must evaluate to an integer or char (not float/double)
  • Case values must be constants (literals or #define values)
  • Each case ends with break (usually)
  • default is optional — runs when no case matches

How switch Works

  1. Evaluate the switch(expression)
  2. Compare result with each case value from top to bottom
  3. On match → jump to that case and start executing
  4. Execute until a break is encountered (or switch ends)
  5. If no match → execute default (if present)

The Role of break

Without break, execution "falls through" into the next case! This is a feature (fall-through) but usually a bug for beginners.

C Language – Without break (Fall-through)
int x = 2;
switch(x) {
    case 1: printf("One\\n");
    case 2: printf("Two\\n");   // Matches here
    case 3: printf("Three\\n"); // Falls through! No break in case 2
    default: printf("Default\\n"); // Also falls through!
}
Two Three Default
⚠️ Always Use break!

Unless you intentionally want fall-through, always put break; at the end of each case. Forgetting break is a very common C bug.

The default Case

default runs when no case matches. It's optional but recommended as a safety net.

Complete Programs

Example 1: Day of Week

C Language
#include <stdio.h>
int main() {
    int day;
    printf("Enter day number (1-7): ");
    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! Enter 1-7\\n");
    }
    return 0;
}
Enter day number (1-7): 5 Friday

Example 2: Calculator using switch

C Language – Switch Calculator
#include <stdio.h>
int main() {
    float a, b;
    char op;
    printf("Enter: num op num (e.g. 5 + 3): ");
    scanf("%f %c %f", &a, &op, &b);
    switch(op) {
        case '+': printf("= %.2f\\n", a + b); break;
        case '-': printf("= %.2f\\n", a - b); break;
        case '*': printf("= %.2f\\n", a * b); break;
        case '/':
            if(b != 0) printf("= %.2f\\n", a / b);
            else printf("Division by zero!\\n");
            break;
        default: printf("Unknown operator\\n");
    }
    return 0;
}
Enter: num op num (e.g. 5 + 3): 10 * 4 = 40.00

Example 3: Vowel or Consonant (char switch)

C Language – Multiple case labels
#include <stdio.h>
int main() {
    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;
}
Enter a letter: A A is a Vowel

switch vs if-else

Featureswitchif-else
Condition typeOnly equality (==) with integers/charsAny condition (range, logical, etc.)
ReadabilityCleaner for many discrete valuesBetter for complex conditions
PerformanceFaster (uses jump table)Slower for many conditions
Float/double❌ Not allowed✅ Allowed
Multiple values✅ Multiple case labelsNeeds OR (||) operator

Summary

  • switch matches an expression against constant case values
  • Without break, execution falls through to next case
  • default runs when no case matches
  • Only works with int and char — not float, double, or strings
  • Multiple case labels can share one code block
  • Use switch when checking one variable against many discrete values
🏋️ Practice

Write: (1) Month name from number (1=January...) (2) Season from month number (3) Simple menu with 4 operations (4) Direction from WASD key input.

switch Statement का Syntax

switch statement एक multi-way decision statement है। यह एक variable को constant values (cases) की list के साथ match करता है और matching block execute करता है।

Syntax
switch (expression) {
    case value1:
        // case 1 का code
        break;
    case value2:
        // case 2 का code
        break;
    default:
        // कोई case match न हो तो
}

switch कैसे काम करता है

  1. switch(expression) evaluate करो
  2. Result को हर case value से ऊपर से नीचे compare करो
  3. Match मिले → उस case पर jump करो और execute शुरू
  4. break मिले तक execute करते रहो
  5. कोई match नहीं → default execute करो

break का Role

break के बिना execution "fall-through" होती है — यानी अगले case में भी चली जाती है! यह beginners के लिए आम bug है।

⚠️ हमेशा break लिखें!

जब तक intentionally fall-through नहीं चाहिए, हर case के अंत में break; ज़रूर लिखें।

Programs

Day of Week

C Language
int day = 5;
switch(day) {
    case 1: printf("Somvar\\n");    break;
    case 2: printf("Mangalvar\\n"); break;
    case 5: printf("Shukravar\\n"); break;
    case 7: printf("Ravivar\\n");   break;
    default: printf("Invalid\\n");
}
Shukravar

Calculator Program

C Language
float a = 10, b = 4;
char op = '*';
switch(op) {
    case '+': printf("= %.2f\\n", a+b); break;
    case '-': printf("= %.2f\\n", a-b); break;
    case '*': printf("= %.2f\\n", a*b); break;
    case '/': printf("= %.2f\\n", a/b); break;
    default:  printf("Invalid operator\\n");
}
= 40.00

सारांश

  • switch expression को constant case values से match करता है
  • break के बिना fall-through होती है — हमेशा break लिखें
  • default तब चलता है जब कोई case match नहीं करता
  • केवल int और char work करते हैं — float/double/string नहीं
  • जब एक variable को many discrete values से check करना हो तो switch use करें
← Back to C Tutorial