> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C Switch Statement

The `switch` statement is a multi-way branching control structure that evaluates a single integral control expression and transfers execution flow to a labeled `case` block matching the evaluated result. It provides an optimized alternative to deeply nested `if-else if` chains when comparing a single variable against multiple discrete constant values.

```c theme={"dark"}
switch (control_expression) {
    case constant_expression_1:
        // statements
        break;
    case constant_expression_2:
        // statements
        break;
    default:
        // statements
}
```

## Structural Rules and Constraints

* **Control Expression:** The expression evaluated by the `switch` must resolve to an integer type (`int`, `char`, `short`, `long`, `enum`, or boolean). Floating-point types (`float`, `double`), pointers, and aggregate types (structs, arrays) are strictly prohibited and will result in a compilation error.
* **Case Labels:** The values following the `case` keyword must be compile-time integer constant expressions. Variables, pointers, or function calls are invalid. Every `case` value within a single `switch` block must be unique.
* **Default Label:** The `default` label is optional and acts as a catch-all if no `case` matches the control expression. A `switch` statement can have at most one `default` label, which can be placed anywhere within the block, though convention dictates placing it at the end.

## Execution Flow and Fallthrough

When a `switch` statement executes, the control expression is evaluated exactly once. The program counter then jumps directly to the `case` label matching that value.

C implements **implicit fallthrough**. Once execution jumps to a matching `case`, it proceeds sequentially through all subsequent statements—ignoring further `case` or `default` labels—until it encounters a `break` statement or reaches the end of the `switch` block.

The `break` keyword is required to terminate the execution of a specific branch and force an immediate exit from the `switch` scope.

## Scope and Variable Initialization

The entire `switch` statement forms a single block scope. However, because the `switch` mechanism acts as a computed `goto`, any variable initialization placed immediately after the `switch` opening brace and before the first `case` label will be bypassed and left uninitialized.

To safely declare and initialize variables within a specific `case`, you must introduce a nested block scope using curly braces `{}`.

```c theme={"dark"}
int control_val = 2;

switch (control_val) {
    // WARNING: Declaration is allowed, but initialization is bypassed.
    int bypassed_var = 10; 

    case 1:
        // Execution skips this branch entirely.
        break;
        
    case 2:
        // Execution jumps here.
        // No break statement; execution will fall through to case 3.
        
    case 3:
        // Executes due to fallthrough from case 2.
        break;
        
    case 4: {
        // Nested block scope required to initialize local variables.
        int scoped_var = 50;
        break;
    }
        
    default:
        // Bypassed because a match was found.
        break;
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
