How do I match multiple values in one case?

← Control Flow · Ref: Q69

EK9 uses comma-separated values in a single case clause. This replaces the fallthrough pattern found in Java, C, and JavaScript.

BASIC MULTI-VALUE CASE

List multiple values separated by commas:

  switch day
    case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
      dayType: "weekday"
    case "Saturday", "Sunday"
      dayType: "weekend"
    default
      dayType: "invalid"

If the value matches ANY of the listed values, the case body executes.

INTEGER MULTI-VALUE

Group numeric codes:

  switch errorCode
    case 400, 401, 403, 404
      category: "client error"
    case 500, 502, 503
      category: "server error"
    default
      category: "other"

MIXING LITERALS AND FUNCTION CALLS

Case values can include function calls alongside literals:

  switch temperature
    case currentTemperature("GB"), 21, 22, 23, 24
      comfort: "Perfect"

The function call is evaluated and compared just like the literals.

WHY NO FALLTHROUGH

Other languages use fallthrough for multi-value matching:

  // Java fallthrough pattern (error-prone)
  case 1:
  case 2:
  case 3:
    result = "low";
    break;  // forget this and bugs happen

EK9 eliminates this entirely. CERT ranks switch fallthrough as the #7 most dangerous coding error. Multiple values per case handles the same use case safely.

COMPLEXITY

Multiple values per case do NOT increase code complexity. 'case 25, 26, 27' is one code path, not three. The complexity is the same as a single-value case.

See Q63 for basic switch. See Q68 for switch as expression. See Q144 for why EK9 has no break/fallthrough.

Example

defines module qa.flow.switch.multivalue

  defines program

    MultiCaseDemo()
      stdout <- Stdout()

      // === WEEKDAY/WEEKEND GROUPING ===

      days <- ["Monday", "Saturday", "Wednesday", "Sunday", "Friday"]
      for day in days
        dayType <- String()
        switch day
          case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
            dayType: "weekday"
          case "Saturday", "Sunday"
            dayType: "weekend"
          default
            dayType: "invalid"
        stdout.println(`${day} -> ${dayType}`)

      // === NUMERIC GROUPING ===

      codes <- [200, 301, 404, 500]
      for code in codes
        category <- String()
        switch code
          case 200, 201, 204
            category: "success"
          case 301, 302, 304
            category: "redirect"
          case 400, 401, 403, 404
            category: "client error"
          case 500, 502, 503
            category: "server error"
          default
            category: "other"
        stdout.println(`HTTP ${code} -> ${category}`)

      // === MULTI-VALUE IN EXPRESSION FORM ===

      season <- 7
      seasonName <- switch season
        <- rtn as String?
        case 3, 4, 5
          rtn: "Spring"
        case 6, 7, 8
          rtn: "Summer"
        case 9, 10, 11
          rtn: "Autumn"
        case 12, 1, 2
          rtn: "Winter"
        default
          rtn: "invalid month"
      stdout.println(`Month ${season} -> ${seasonName}`)

Common mistakes

E07320 — EK9 requires a 'default' clause in switch statements to ensure all cases are handled. Omitting it triggers E07320. This prevents bugs where unexpected values silently fall through. See ek9 -h E07320 for details.

Incorrect:

//no default needed

Correct:

default
            dayType: "invalid"

E01070 — EK9 has no break statement — it does not exist in the language. Each case clause is self-contained with no fallthrough. The break keyword is deliberately excluded. See ek9 -h E01070 for details.

Incorrect:

case 200, 201, 204
            category: "success"
            break

Correct:

case 200, 201, 204
            category: "success"
Other ways to ask this
  • How do I handle multiple case values in EK9?
  • How does EK9 replace switch fallthrough?
  • Can I list several values in a single case clause?

Coming from another language?

Java: fallthrough by default, must add break to prevent it, switch expressions use arrow syntax since Java 14. Python: case X | Y in match/case since 3.10, uses pipe operator. Rust: pattern1 | pattern2 in match arms, uses pipe operator. Go: case X, Y with comma-separated values and no fallthrough by default. C/C++: fallthrough by default, major bug source, CERT #7 most dangerous error. Kotlin: when with comma-separated values, no fallthrough. C#: no fallthrough, multiple labels with goto case (complex). JavaScript: fallthrough by default like Java. Swift: case X, Y with comma-separated values, no fallthrough. EK9: case X, Y with comma-separated values, no fallthrough exists, no break keyword.

Keywords: match, flow, branch, control, fallthrough, condition, values, group, break, migrate, several, multiple, comma, case