> ## 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.

# Dart Switch Statement

A `switch` statement is a branching control flow construct that evaluates a given expression against a series of `case` clauses. When a match is found, the statement executes the associated block of code. In Dart 3.0 and later, `switch` statements fully support pattern matching, allowing for complex destructuring, logical operators, and guard clauses.

```dart theme={"dark"}
switch (expression) {
  case pattern1:
    // Statements executed if expression matches pattern1
  case pattern2 when condition:
    // Statements executed if expression matches pattern2 AND condition is true
  default:
    // Statements executed if no patterns match
}
```

## Execution Mechanics and Rules

* **Evaluation:** The target `expression` is evaluated exactly once.
* **Matching:** Cases are evaluated sequentially from top to bottom. Dart utilizes pattern matching semantics to determine a match. If no patterns match, the `default` clause (or the wildcard pattern `case _:`) is executed.
* **Implicit Break (Dart 3.0+):** `break` statements are no longer required at the end of a `case` block. Dart automatically breaks out of the `switch` statement after executing a matched, non-empty case.
* **Empty Case Fallthrough:** Implicit fallthrough is strictly limited to empty `case` clauses. If a `case` has no statements, execution falls through to the next `case`.

```dart theme={"dark"}
switch (command) {
  case 'OPEN':
  case 'START': // Implicit fallthrough allowed because 'OPEN' is empty
    executeStart();
  case 'CLOSE':
    executeClose(); // Implicit break occurs here
}
```

## Explicit Fallthrough (Labeled Cases)

To achieve fallthrough across non-empty cases, Dart requires explicit control flow using the `continue` keyword combined with a label.

```dart theme={"dark"}
switch (status) {
  case 'PENDING':
    print('Status is pending.');
    continue processLabel; // Explicitly jumps to processLabel
    
  processLabel:
  case 'PROCESSING':
    print('Status is processing.');
}
```

## Pattern Matching and Logical Operators

Dart allows the use of logical operators (`||`, `&&`) and relational operators directly within `case` clauses to match multiple conditions without relying on empty case fallthrough.

```dart theme={"dark"}
switch (numericValue) {
  case 1 || 2 || 3:
    print('Matches 1, 2, or 3');
  case > 10 && < 20:
    print('Matches between 11 and 19');
}
```

## Guard Clauses

A `case` can be appended with a `when` keyword followed by a boolean expression. This is known as a guard clause. The `case` will only match if both the pattern matches the expression and the guard clause evaluates to `true`.

```dart theme={"dark"}
switch (point) {
  case (int x, int y) when x == y:
    print('Coordinates are identical.');
  case (int x, int y):
    print('Coordinates are different.');
}
```

## Exhaustiveness Checking

When switching over algebraic data types—such as `enum` types, `sealed` classes, or `bool`—the Dart analyzer enforces exhaustiveness. The compiler will throw an error if the `switch` statement does not account for every possible value or subtype, unless a `default` or wildcard (`_`) clause is provided.

```dart theme={"dark"}
sealed class Shape {}
class Circle extends Shape {}
class Square extends Shape {}

// The compiler enforces that both Circle and Square are handled.
switch (shape) {
  case Circle():
    // Handle circle
  case Square():
    // Handle square
}
```

<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 Dart 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>
