C Programming • C Basics, Operators, Loops
C Programming / Control Structures: switch-case

Control Structures: switch-case

Notes 2 C Basics, Operators, Loops

Definition: The switch-case statement is a decision-making control structure used to select one block of statements from multiple possible choices based on the value of an expression.

Notes

Control Structure: switch-case

Definition: The switch-case statement is a decision-making control structure used to select one block of statements from multiple possible choices based on the value of an expression.

💡 Example: A program can use switch-case to display a day name based on a number from 1 to 7.

Syntax

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

    case value2:
        statement;
        break;

    default:
        statement;
}

Important Parts

case: Defines one possible value to compare with the switch expression.

💡 Example: case 1: can represent Monday when the input is 1.

break: Ends the current case and prevents execution from continuing into the next case.

💡 Example: After displaying Monday, break; stops the switch from executing the next case.

default: Executes when none of the specified case values match the expression.

💡 Example: An input such as 9 does not match cases 1 to 7, so the default block executes.

How switch-case Works

Expression ↓ Compare with case values ↓ Matching case executes ↓ break ↓ Switch ends

Real-World Example

A menu-driven application can use switch-case when the user has to choose one option from a fixed list of choices.

💡 Example: 1 → Add, 2 → Subtract, 3 → Multiply.

Practical Example — Displaying the Day of the Week

Problem Statement

Write a C program that accepts a number from 1 to 7 and displays the corresponding day of the week using switch-case.

Learning Outcomes

  • Use the switch-case construct to select among several fixed outcomes.
  • Understand the use of the break statement.
  • Use default to handle invalid input.

Hint

Map 1 to Monday, 2 to Tuesday, and so on up to 7 to Sunday. Use default for values outside this range.

Theory

The switch statement compares one expression with a list of constant case values and executes the matching case. The break statement ends the case, while default handles values that do not match any case.

Program

#include <stdio.h>

int main()
{
    int day;

    printf("Enter a number (1-7): ");
    scanf("%d", &day);

    // match the input number to the corresponding 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 input
");
    }

    return 0;
}

Expected Output

Enter a number (1-7): 3
Wednesday

Note

Forgetting break can cause fall-through, where execution continues into the next case. The default case is useful for handling invalid input.

switch-case vs if-else

switch-case if-else
Useful when one expression is compared with fixed values. Useful for conditions and ranges.
Uses case, break and default. Uses if, else if and else.
Good for menu-driven choices. Good for relational and logical conditions.

Quick Revision

🎯 Remember:

switch → checks the expression
case → represents a possible value
break → exits the current case
default → handles unmatched values

Important Exam Questions

Short Answer Questions

  1. What is a switch-case statement in C?
  2. Write the syntax of switch-case.
  3. What is the purpose of the case statement?
  4. What is the use of break in switch-case?
  5. What is the purpose of default?
  6. What is fall-through in switch-case?

Long Answer Questions

  1. Explain the switch-case statement with syntax and suitable example.
  2. Write a C program using switch-case to display the day of the week.
  3. Explain the role of case, break and default in switch-case.
🎥 Recommended Learning

Watch a beginner-friendly explanation of switch-case in C.

▶ Watch: switch-case in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for switch-case will be provided here.

🧠 Mind Map

Use the mind map for quick revision of switch, case, break and default.