📚 C Programming Tutorial

C Programming

Learn C programming concepts with simple explanations, examples and practical notes.

← Back to C Programming

Switch Statements

What is a switch statement?

The switch statement is a decision-making statement in C that allows a program to select and execute one block of code from several alternatives based on the value of a single variable or of a simple expression (called the controlling expression). The value of this expression may be of type int or char, but not of type float or double.

For example, suppose a user enters a number from 1 to 3:
- If the number is 1, display One.
- If the number is 2, display Two.
- If the number is 3, display Three.
- Otherwise, display Invalid choice.
A switch statement is useful when we need to compare one expression against several fixed values.

Syntax of the switch statement


switch (expression)
{
    case constant1: statements;
			break;
    case constant2: statements;
			break;
    case constant3: statements;
			break;
    default: statements;
}

Note: constant can be ither numaric values or charecter values

How does a switch statement work?

How does a switch statement work flowchart

Example 1: C Program to display a day using switch

#include 

int main(void)
{
    int day;

    printf("Enter a day number (1-7): ");
    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 number");
    }

    return 0;
}

Why is break important?

The break statement stops execution of the current switch statement and transfers control to the statement following it. Consider this program without break:

What is the purpose of default?

The default block executes when none of the case labels matches the value of the expression.

Output: Enter a day number (1-7): 10
		Invalid day number

C Program for Simple calculator using switch-case (arthmetic operations using switch)

#include 

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

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

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

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

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

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

        case '/': if (b != 0)
                     printf("Result = %.2f", a / b);
                  else
                     printf("Division by zero is not allowed");
                  break;

        default: printf("Invalid operator");
    }

    return 0;
}

Rules and limitations of switch-case

  • case labels must be constant expressions that can be used as integer constant expressions.
  • Duplicate case values are not allowed.
  • The default label is optional.
  • break exits the nearest enclosing switch or loop, not just the current case.
  • Unlike an if condition, a case label cannot directly test a range such as case 1 to 5 in standard C.
  • Strings and floating-point values cannot be used directly as switch expressions in standard C.

Difference between if-else and switch

if-else switch
Tests conditions or expressions. Selects among matching constant case values.
Supports relational operators such as <, >, and <=. Does not directly test relational ranges in case labels.
Can test complex logical conditions. Commonly tests one expression against fixed values.
Suitable for ranges and complex decisions. Suitable for menus and fixed choices.
Uses else as the final alternative. Uses optional default as the unmatched alternative.

Practice questions

  1. Define the switch statement and explain its syntax.
  2. Explain the purpose of case, break, and default.
  3. Write a C program to display the day of the week using switch.
  4. Write a C program to perform arithmetic operations using switch.
  5. Differentiate between if-else and switch-case.
  6. Explain the rules and limitations of a switch statement.