How do I use comparison operators in switch cases?

← Control Flow · Ref: Q70

EK9 switch cases can use comparison operators. Instead of matching exact values, you can write 'case < 5', 'case > 100', 'case >= threshold' and similar. The operator is applied to the switch control variable.

BASIC COMPARISON CASES

Use less-than and greater-than in cases:

  switch temperature
    case < 0
      label: "freezing"
    case < 15
      label: "cold"
    case < 25
      label: "comfortable"
    default
      label: "hot"

Order matters: the first matching case wins, just like if/else-if.

SUPPORTED OPERATORS

All comparison operators work in cases:

  case < 5       less than
  case > 100     greater than
  case <= 10     less than or equal
  case >= 90     greater than or equal
  case == 42     explicit equality
  case <> 0      not equal

EXPRESSIONS IN COMPARISONS

The right side of the operator can be any expression, including arithmetic and function calls:

  multiplier <- 5
  switch conditionValue
    case > 10 * multiplier
      label: "Very High"
    case < 12
      label: "Moderate"
    default
      label: "Normal"

MIXING WITH LITERALS

You can mix comparison cases with literal value cases in the same switch:

  switch score
    case < 0
      label: "invalid"
    case 0
      label: "zero"
    case > 100
      label: "over maximum"
    default
      label: "valid"

ORDER MATTERS

Cases are evaluated top to bottom. First match wins. Place more specific cases before general ones to avoid shadowing:

  switch temperature
    case < 0        checked first
    case < 15       checked second (only if not less than 0)
    default         everything else

Switch with comparisons works in expression form too (see Q68). Multiple values per case can be mixed with comparisons (see Q69).

See Q63 for basic switch. See Q68 for switch as expression. See Q71 for pattern matching in switch.

Example

defines module qa.flow.switch.operators

  defines function

    supplyMultiplier()
      <- rtn <- 10

    supplyCheckValue()
      <- rtn <- 55

  defines program

    SwitchComparisonDemo()
      stdout <- Stdout()

      // === TEMPERATURE CLASSIFICATION ===

      temperatures <- [35, 15, -5, 22, 0]
      for temperature in temperatures
        classification <- String()
        switch temperature
          case < 0
            classification: "freezing"
          case < 10
            classification: "cold"
          case < 20
            classification: "cool"
          case < 30
            classification: "warm"
          default
            classification: "hot"
        stdout.println(`${temperature}C -> ${classification}`)

      // === GRADE CLASSIFICATION WITH EXPRESSION ===

      scores <- [95, 82, 71, 55, 40]
      for score in scores
        grade <- switch score
          <- rtn as String?
          case >= 90
            rtn: "A"
          case >= 80
            rtn: "B"
          case >= 70
            rtn: "C"
          case >= 60
            rtn: "D"
          default
            rtn: "F"
        stdout.println(`Score ${score} -> Grade ${grade}`)

      // === MIXING COMPARISONS WITH LITERALS ===

      testValues <- [-1, 0, 50, 100, 150]
      for testValue in testValues
        category <- String()
        switch testValue
          case < 0
            category: "negative"
          case 0
            category: "zero"
          case 100
            category: "perfect"
          case > 100
            category: "over limit"
          default
            category: "normal"
        stdout.println(`${testValue} -> ${category}`)

      // === EXPRESSIONS IN COMPARISONS ===

      multiplier <- supplyMultiplier()
      checkValue <- supplyCheckValue()
      assessment <- switch checkValue
        <- rtn as String?
        case > 10 * multiplier
          rtn: "exceeds scaled limit"
        case > multiplier
          rtn: "above base"
        default
          rtn: "within range"
      stdout.println(`${checkValue} -> ${assessment}`)

Common mistakes

E01070 — EK9 has no break statement. Switch cases with comparison operators are self-contained like all other case clauses. No fallthrough exists. See ek9 -h E01070 for details.

Incorrect:

switch temperature
          case < 0
            classification: "freezing"
            break

Correct:

switch temperature
          case < 0
            classification: "freezing"
Other ways to ask this
  • Can I use less than or greater than in a case clause?
  • How do I match ranges in a switch statement?
  • How do comparison operators work in EK9 switch cases?

Coming from another language?

Java: no comparison operators in case labels, must use if/else-if chains. C#: relational patterns since C# 9 (case < 12 in switch expressions), closest to EK9. Rust: match guards with if (match x { n if n < 5 => ... }), separate from pattern. Python: no comparison in match/case, use if/elif. Go: no comparison in case, use if/else. Kotlin: when supports arbitrary boolean expressions (in 1..10, is Type), very flexible. C/C++: no comparison in case, constants only. JavaScript: no comparison in case labels. Swift: where clause for conditions (case let x where x < 5). EK9: comparison operators directly in case clause, mixed with literals and function calls, no separate guard syntax needed.

Keywords: threshold, case, condition, inequality, switch, range, comparison, flow, greater, less, operator, control, branch