Lesson 27 of 60 – switch Statement in C
45%

switch Statement in C

The switch statement is a decision-making statement in C that allows a program to choose one block of code from multiple possible options. It is especially useful when a single expression needs to be compared with several constant values.

Note: A switch statement normally uses case labels and a break statement. The optional default block executes when no case matches.

1. What is a switch Statement?

The switch statement provides multiple choices based on the value of an expression.

switch (expression)
{
    case value1:
        statement;
        break;

    case value2:
        statement;
        break;

    default:
        statement;
}

2. Why Use switch?

A switch statement is useful when one expression needs to be compared against several discrete constant values.

int day = 2;

switch (day)
{
    case 1:
        printf("Monday");
        break;

    case 2:
        printf("Tuesday");
        break;
}

This can be easier to read than a long sequence of equality comparisons.

3. Basic Syntax of switch

switch (expression)
{
    case constant1:
        // statements
        break;

    case constant2:
        // statements
        break;

    default:
        // statements
}

The expression is evaluated and compared with the case constants.

4. Understanding case

A case label specifies a possible constant value for the switch expression.

int choice = 2;

switch (choice)
{
    case 1:
        printf("One");
        break;

    case 2:
        printf("Two");
        break;
}

Because choice is 2, the second case is selected.

5. Understanding break

The break statement terminates the switch statement and continues execution after it.

switch (choice)
{
    case 1:
        printf("One");
        break;

    case 2:
        printf("Two");
        break;
}

Using break prevents execution from continuing into the next case.

6. Simple switch Example

int number = 2;

switch (number)
{
    case 1:
        printf("One");
        break;

    case 2:
        printf("Two");
        break;

    case 3:
        printf("Three");
        break;
}

Output:

Two

7. Using default

The default label is optional. It is used when none of the case values matches the switch expression.

int choice = 5;

switch (choice)
{
    case 1:
        printf("One");
        break;

    case 2:
        printf("Two");
        break;

    default:
        printf("Invalid choice");
}

Since there is no case 5, the default block executes.

8. switch with User Input

#include <stdio.h>

int main()
{
    int choice;

    printf("Enter a number from 1 to 3: ");
    scanf("%d", &choice);

    switch (choice)
    {
        case 1:
            printf("You selected One");
            break;

        case 2:
            printf("You selected Two");
            break;

        case 3:
            printf("You selected Three");
            break;

        default:
            printf("Invalid choice");
    }

    return 0;
}

9. switch with Character

A character can be used as the switch expression.

char grade = 'A';

switch (grade)
{
    case 'A':
        printf("Excellent");
        break;

    case 'B':
        printf("Very Good");
        break;

    case 'C':
        printf("Good");
        break;

    default:
        printf("Invalid grade");
}

10. switch for Days

int day = 3;

switch (day)
{
    case 1:
        printf("Monday");
        break;

    case 2:
        printf("Tuesday");
        break;

    case 3:
        printf("Wednesday");
        break;

    case 4:
        printf("Thursday");
        break;

    case 5:
        printf("Friday");
        break;

    default:
        printf("Invalid day");
}

Each case represents a different day number.

11. switch for Menu Selection

int choice = 2;

switch (choice)
{
    case 1:
        printf("Add Student");
        break;

    case 2:
        printf("View Student");
        break;

    case 3:
        printf("Delete Student");
        break;

    default:
        printf("Invalid choice");
}

Switch statements are commonly used for menu-based programs.

12. switch Calculator

int a = 20;
int b = 5;
char op = '+';

switch (op)
{
    case '+':
        printf("%d", a + b);
        break;

    case '-':
        printf("%d", a - b);
        break;

    case '*':
        printf("%d", a * b);
        break;

    case '/':
        if (b != 0)
        {
            printf("%d", a / b);
        }
        else
        {
            printf("Cannot divide by zero");
        }
        break;

    default:
        printf("Invalid operator");
}

13. Multiple Statements in a Case

int choice = 1;

switch (choice)
{
    case 1:
        printf("Welcome\n");
        printf("You selected option 1\n");
        break;

    case 2:
        printf("You selected option 2");
        break;
}

A case can contain multiple statements.

14. Multiple Cases with the Same Action

Multiple case labels can share the same block of statements.

char grade = 'A';

switch (grade)
{
    case 'A':
    case 'B':
        printf("Good grade");
        break;

    case 'C':
        printf("Average grade");
        break;

    default:
        printf("Other grade");
}

Both A and B reach the same statement.

15. Fall-Through Behavior

If a case does not contain a break, execution continues into the next case. This is called fall-through.

int number = 1;

switch (number)
{
    case 1:
        printf("One\n");

    case 2:
        printf("Two\n");
        break;
}

For number = 1, both messages are printed because the first case has no break.

16. Intentional Fall-Through

Fall-through can be intentionally used when several cases should perform the same operation.

int month = 1;

switch (month)
{
    case 1:
    case 2:
    case 3:
        printf("First Quarter");
        break;

    default:
        printf("Other Quarter");
}

Cases 1, 2, and 3 all reach the same block.

17. switch vs if-else

Both switch and if-else can be used for decision making, but they are useful in different situations.

A switch is convenient when comparing one expression with several discrete constant values.

switch (choice)
{
    case 1:
        printf("One");
        break;

    case 2:
        printf("Two");
        break;
}

An if-else structure is more flexible for ranges and complex conditions.

18. switch Cannot Directly Check Ranges

A switch is designed for matching case values, not for directly expressing conditions such as marks >= 80.

For example, range-based grading is naturally written using if-else:

if (marks >= 80)
{
    printf("A");
}
else if (marks >= 60)
{
    printf("B");
}

This is generally more suitable than trying to use a switch for ranges.

19. switch with Integer Values

int number = 10;

switch (number)
{
    case 5:
        printf("Five");
        break;

    case 10:
        printf("Ten");
        break;

    case 15:
        printf("Fifteen");
        break;

    default:
        printf("Other number");
}

Integer expressions are commonly used with switch statements.

20. switch with enum Values

enum Day
{
    MONDAY,
    TUESDAY,
    WEDNESDAY
};

enum Day day = TUESDAY;

switch (day)
{
    case MONDAY:
        printf("Monday");
        break;

    case TUESDAY:
        printf("Tuesday");
        break;

    case WEDNESDAY:
        printf("Wednesday");
        break;
}

Enumeration constants can also be used as case labels.

21. Case Labels Must Be Constant Expressions

A case label must use a constant expression that is valid for the switch selection.

int number = 2;

switch (number)
{
    case 1:
        printf("One");
        break;

    case 2:
        printf("Two");
        break;
}

Case labels are not normally written using runtime variables such as case x: when x is an ordinary variable.

22. Common Mistake: Forgetting break

A common beginner mistake is forgetting break.

switch (choice)
{
    case 1:
        printf("One");

    case 2:
        printf("Two");
        break;
}

If choice is 1, both messages can be printed because execution falls through into case 2.

23. Common Mistake: Duplicate Case Values

The same case value should not be repeated in one switch statement.

switch (number)
{
    case 1:
        printf("One");
        break;

    /* case 1 again would be invalid */
}

Each case value must be unique within the same switch statement.

24. Common Mistake: Using Incorrect Data Types

The controlling expression of a traditional C switch should have an integral or enumeration type.

int choice = 2;

switch (choice)
{
    case 1:
        printf("One");
        break;

    case 2:
        printf("Two");
        break;
}

For floating-point range comparisons, an if-else structure is usually the appropriate choice.

25. switch with Nested if

A switch case can contain an if statement when additional conditions are required.

int choice = 1;
int age = 20;

switch (choice)
{
    case 1:
        if (age >= 18)
        {
            printf("Adult");
        }
        else
        {
            printf("Minor");
        }
        break;

    default:
        printf("Invalid choice");
}

26. Complete Menu Program

#include <stdio.h>

int main()
{
    int choice;

    printf("1. Add\n");
    printf("2. View\n");
    printf("3. Update\n");
    printf("4. Delete\n");

    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice)
    {
        case 1:
            printf("Add selected");
            break;

        case 2:
            printf("View selected");
            break;

        case 3:
            printf("Update selected");
            break;

        case 4:
            printf("Delete selected");
            break;

        default:
            printf("Invalid choice");
    }

    return 0;
}

27. Complete Calculator Program

#include <stdio.h>

int main()
{
    int a, b;
    char op;

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

    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &op);

    switch (op)
    {
        case '+':
            printf("Result = %d", a + b);
            break;

        case '-':
            printf("Result = %d", a - b);
            break;

        case '*':
            printf("Result = %d", a * b);
            break;

        case '/':
            if (b != 0)
            {
                printf("Result = %d", a / b);
            }
            else
            {
                printf("Cannot divide by zero");
            }
            break;

        default:
            printf("Invalid operator");
    }

    return 0;
}

28. Complete Day Program

#include <stdio.h>

int main()
{
    int day;

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

    switch (day)
    {
        case 1:
            printf("Monday");
            break;

        case 2:
            printf("Tuesday");
            break;

        case 3:
            printf("Wednesday");
            break;

        case 4:
            printf("Thursday");
            break;

        case 5:
            printf("Friday");
            break;

        case 6:
            printf("Saturday");
            break;

        case 7:
            printf("Sunday");
            break;

        default:
            printf("Invalid day");
    }

    return 0;
}

29. When to Use switch

Use a switch when:

  • You are comparing one expression with several discrete values.
  • You are creating menu-based programs.
  • You are handling commands or character choices.
  • You need readable alternatives for fixed constant options.
  • You want multiple cases to share the same code.

Use if-else when you need ranges, complex logical conditions, or comparisons that are not naturally represented by fixed case values.

30. Practice Programs Using switch

Practice the following programs:

  1. Create a day-of-week program.
  2. Create a month-selection program.
  3. Create a simple calculator.
  4. Create a menu-driven student program.
  5. Display grades based on character input.
  6. Create a simple food menu.
  7. Create a program for arithmetic operations.
  8. Use multiple cases for weekdays and weekends.
  9. Create a basic banking menu.
  10. Create a program that displays different messages for numbers 1 to 5.

Always remember to use break when you want each case to execute independently. Practice intentional fall-through separately.

📌 Key Points

  • The switch statement is used for multiple fixed choices.
  • Each option is represented by a case label.
  • The break statement normally stops execution from falling into the next case.
  • The default case handles unmatched values and is optional.
  • Multiple case labels can share the same block.
  • Missing break can cause fall-through.
  • Fall-through can also be used intentionally.
  • Case labels must use appropriate constant expressions.
  • Switch is useful for menus, commands, days, choices, and fixed options.
  • if-else is generally more suitable for ranges and complex conditions.

🧠 Quick Quiz

Question: Which keyword is commonly used to stop execution from continuing into the next case of a switch statement?