Skip to main content

case

Case statements are used within switch constructs to define different execution paths based on matching values. Each case specifies one or more values to match against and the code to execute when matched.

Basic Case Usage

Cases are used inside switch statements:

Multiple Values per Case

A single case can match multiple values:

Case with Different Data Types

Cases can match different types of values:

Case with Expressions

Cases can use expressions as values:

Case with Complex Logic

Each case can contain complex code blocks:

Nested Switch Cases

Cases can contain nested switch statements:

Fall-through Behavior

Without break, cases fall through to the next case:

Case with Function Calls

Cases can call functions:

Case with Variable Assignments

Cases can perform variable assignments:

Best Practices

  1. Always use break: Prevent unintended fall-through
  2. Include default case: Handle unexpected values
  3. Keep cases simple: Complex logic should be in functions
  4. Group related values: Use multiple values per case when logical
Case statements provide a clean and efficient way to handle multiple conditional branches in Soul applications.