Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Understanding Switch‑Case in C: Syntax, Examples, and Best Practices

What Is the Switch Statement in C?

The switch construct evaluates a single expression and routes program flow to the matching case label. If no match is found, the optional default block runs. This decision‑making tool is especially useful when handling discrete, constant values such as menu options, error codes, or user selections.

Each case is uniquely identified by a constant expression. When the expression’s value matches a case label, the corresponding block of statements executes, and execution exits the switch (unless a break is omitted, which causes fall‑through).

Switch‑Case Syntax

switch (expression) {
    case constant1:
        // statements
        break;
    case constant2:
        // statements
        break;
    // …
    default:
        // statements
        break;
}
// statements following the switch

How the Switch Statement Works

The following flowchart illustrates the decision process for a switch statement:

Understanding Switch‑Case in C: Syntax, Examples, and Best Practices

Example 1: Basic Switch

#include <stdio.h>
int main() {
    int num = 8;
    switch (num) {
        case 7:
            printf("Value is 7");
            break;
        case 8:
            printf("Value is 8");
            break;
        case 9:
            printf("Value is 9");
            break;
        default:
            printf("Out of range");
            break;
    }
    return 0;
}

Output:

Value is 8

Example 2: Using Default

#include <stdio.h>
int main() {
    int language = 10;
    switch (language) {
        case 1:
            printf("C#\n");
            break;
        case 2:
            printf("C\n");
            break;
        case 3:
            printf("C++\n");
            break;
        default:
            printf("Other programming language\n");
    }
    return 0;
}

Output:

Other programming language

Example 3: Combined Cases

#include <stdio.h>
int main() {
    int number = 5;
    switch (number) {
        case 1:
        case 2:
        case 3:
            printf("One, Two, or Three.\n");
            break;
        case 4:
        case 5:
        case 6:
            printf("Four, Five, or Six.\n");
            break;
        default:
            printf("Greater than Six.\n");
    }
    return 0;
}

Output:

Four, Five, or Six.

Nested Switches

Switches can be nested, allowing hierarchical decision making. The following example demonstrates a simple authentication flow:

#include <stdio.h>
int main() {
    int ID, password;
    printf("Please Enter Your ID:\n");
    scanf("%d", &ID);
    switch (ID) {
        case 500:
            printf("Enter your password:\n");
            scanf("%d", &password);
            switch (password) {
                case 0:
                    printf("Welcome Dear Programmer\n");
                    break;
                default:
                    printf("Incorrect password\n");
            }
            break;
        default:
            printf("Incorrect ID\n");
    }
    return 0;
}

OUTPUT:

 Please Enter Your ID:
 500
 Enter your password:
 000
 Welcome Dear Programmer
Understanding Switch‑Case in C: Syntax, Examples, and Best Practices

Why Switch Is Preferable to Extensive If‑Else

While if‑else chains can achieve the same logic, they grow unwieldy as the number of options increases. Switch statements offer clearer syntax, better readability, and can be optimized by the compiler into jump tables for constant expressions.

Rules for Using Switch

Key Takeaways

C Language

  1. Circuit With a Switch: A Practical Guide to Basic Electrical Circuits
  2. Mastering the C# Switch Statement: Syntax, Examples & Best Practices
  3. Mastering the C++ Switch‑Case Statement: Syntax, Workflow, and Practical Examples
  4. Mastering the C Switch Statement: Syntax, Flow, and Practical Example
  5. Mastering the Java Switch Statement: Syntax, Usage, and Best Practices
  6. Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples
  7. C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples
  8. Java Switch‑Case Statement Explained: Syntax, Examples, and Best Practices
  9. Python Print() Function: A Practical Guide with Examples
  10. Understanding Verilog Case Statements: Efficient Multiplexer Implementation