> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/NormandoRamirezDelgado/6C-Febrero-2026/llms.txt
> Use this file to discover all available pages before exploring further.

# Switch Statements

> Learn how to use switch statements for multi-way branching in Dart

## Overview

Switch statements provide a clean way to handle multiple conditions based on the value of an expression. They are particularly useful when you need to compare a single value against multiple possible matches.

## Basic Syntax

```dart theme={null}
switch (expression) {
  case value1:
    // Code to execute
    break;
  case value2:
    // Code to execute
    break;
  default:
    // Code to execute if no case matches
}
```

<Note>
  Each case must end with `break`, `continue`, `return`, or `throw`. Otherwise, execution will fall through to the next case.
</Note>

## String Matching Example

Switch statements work well with strings:

```dart theme={null}
void main() {
  String dia = 'sábado';

  switch (dia) {
    case 'lunes':
      print('Inicio de Semana');
      break;
    case 'sábado':
      break;
    case 'viernes':
      print('Casi es fin de semana');
      break;
    case 'domingo':
      print('Fin de Semana');
      break;
    default:
      print('Día inválido!');
  }
}
```

## Integer Matching Example

Switch statements can also match integer values:

```dart theme={null}
int calificacion = 8;

switch (calificacion) {
  case 6:
    print('Suficiente');
    break;
  case 7:
    print('Bueno');
    break;
  case 8:
    print('Muy Bueno');
    break;
  case 9:
    print('Sobresaliente');
    break;
  case 10:
    print('Excelente');
    break;
  default:
    print('No Suficiente');
}
// Output: Muy Bueno
```

## Pattern Matching (Dart 3.0+)

<Tip>
  Dart 3.0 introduced enhanced pattern matching in switch statements, allowing for more complex conditions.
</Tip>

```dart theme={null}
int calificacion = 0;

switch (calificacion) {
  case >= 0 && <= 5:
    print('No Suficiente');
    break;
  case 6:
    print('Suficiente');
    break;
  case 7:
    print('Bueno');
    break;
  case 8:
    print('Muy Bueno');
    break;
  case 9:
    print('Sobresaliente');
    break;
  case 10:
    print('Excelente');
    break;
}
```

## Complete Example

```dart theme={null}
void main() {
  String dia = 'sábado';

  switch (dia) {
    case 'lunes':
      print('Inicio de Semana');
      break;
    case 'sábado':
      break;
    case 'viernes':
      print('Casi es fin de semana');
      break;
    case 'domingo':
      print('Fin de Semana');
      break;
    default:
      print('Día inválido!');
  }

  int calificacion = 0;
  switch (calificacion) {
    case >= 0 && <= 5:
      print('No Suficiente');
      break;
    case 6:
      print('Suficiente');
      break;
    case 7:
      print('Bueno');
      break;
    case 8:
      print('Muy Bueno');
      break;
    case 9:
      print('Sobresaliente');
      break;
    case 10:
      print('Excelente');
      break;
  }
}
```

## When to Use Switch vs If-Else

<Steps>
  <Step title="Use switch for discrete values">
    Switch statements are ideal when checking a single variable against multiple specific values.
  </Step>

  <Step title="Use if-else for ranges">
    If-else statements are better for complex conditions or range checks (though Dart 3.0+ supports patterns in switch).
  </Step>

  <Step title="Consider readability">
    Choose the option that makes your code most readable for the specific use case.
  </Step>
</Steps>

## Important Notes

<Warning>
  Every case in a switch statement must have a terminating statement (`break`, `continue`, `return`, or `throw`). Empty cases can fall through to the next case.
</Warning>

## Supported Types

Switch statements in Dart work with:

* Integers
* Strings
* Enums
* Compile-time constants
* Objects (with proper equality implementation)

## Default Case

The `default` case is optional but recommended. It handles any value that doesn't match the specified cases:

```dart theme={null}
switch (value) {
  case 'expected':
    print('Matched!');
    break;
  default:
    print('No match found');
}
```
