How do if/else statements work in EK9?

← Control Flow · Ref: Q61

EK9's if/else works like most languages but with a few differences: no parentheses around conditions, indentation-based blocks (no braces), and no 'elif' keyword (use 'else if' as two words).

BASIC IF

The simplest form tests a condition and executes the indented body:

  if temperature > 30
    stdout.println("Hot day")

No parentheses. No braces. Just condition and indented body.

IF/ELSE

Add an else branch for the alternative path:

  if balance > 0
    status: "positive"
  else
    status: "overdrawn"

The else keyword sits at the same indentation as the if keyword.

IF/ELSE-IF/ELSE

Chain conditions with 'else if' (two words, never 'elif'):

  if score >= 90
    grade: "A"
  else if score >= 80
    grade: "B"
  else if score >= 70
    grade: "C"
  else
    grade: "F"

Conditions are evaluated top to bottom. The first match wins.

CONDITION TYPES

Any expression that produces a Boolean can be a condition:

  if ready                         single Boolean variable
  if count > 0                     comparison
  if name == "Admin"               equality
  if age >= 18 and hasConsent      compound with 'and'
  if isActive or isAdmin           compound with 'or'
  if not finished                  negation

ALTERNATIVE KEYWORD: WHEN

EK9 allows 'when' as an alternative to 'if'. They are interchangeable:

  when temperature > 30
    stdout.println("Hot day")

This is purely stylistic. Use whichever reads better in context.

WHAT IF/ELSE DOES NOT DO IN EK9

EK9's if is a statement, not an expression. You cannot write:

  result <- if condition then "yes" else "no"

For expression-based branching, use switch as an expression (see Q68).

GUARDS AND MORE

EK9's if supports guard variables that combine assignment with condition checking. This is covered in Q74. Guards are what make EK9's if truly powerful, but the basic form shown here works exactly as you would expect from any language.

See Q29 for how unset values interact with conditions. See Q30 for Boolean operators (and, or, xor, not). See Q62 for complex if/else-if chaining patterns. See Q72 for the 'when' keyword in other contexts. See Q74 for guard variables in if statements.

Example

defines module qa.flow.conditional

  defines function

    supplyTemperature() as pure
      <- rtn <- 35

    supplyBalance() as pure
      <- rtn <- -50

    supplyScore() as pure
      <- rtn <- 85

    supplyAge() as pure
      <- rtn <- 25

    supplyLicenseStatus() as pure
      <- rtn <- true

    supplyAdminStatus() as pure
      <- rtn <- false

    supplyOwnerStatus() as pure
      <- rtn <- true

    supplyFinishedStatus() as pure
      <- rtn <- false

  defines program

    IfElseDemo()
      stdout <- Stdout()

      // === BASIC IF ===

      temperature <- supplyTemperature()
      temperatureThreshold <- 30
      if temperature > temperatureThreshold
        stdout.println("Hot day")

      // === IF/ELSE ===

      balance <- supplyBalance()
      status <- String()
      if balance > 0
        status: "positive"
      else
        status: "overdrawn"
      stdout.println(`Balance status: ${status}`)

      // === IF/ELSE-IF/ELSE ===

      score <- supplyScore()
      grade <- String()
      excellentScore <- 90
      goodScore <- 80
      passingScore <- 70
      if score >= excellentScore
        grade: "A"
      else if score >= goodScore
        grade: "B"
      else if score >= passingScore
        grade: "C"
      else
        grade: "F"
      stdout.println(`Score ${score} is grade ${grade}`)

      // === COMPOUND CONDITIONS ===

      age <- supplyAge()
      hasLicense <- supplyLicenseStatus()
      adultAge <- 18
      if age >= adultAge and hasLicense
        stdout.println("Can drive")

      isAdmin <- supplyAdminStatus()
      isOwner <- supplyOwnerStatus()
      if isAdmin or isOwner
        stdout.println("Has access")

      // === NEGATION ===

      finished <- supplyFinishedStatus()
      if not finished
        stdout.println("Still working")

      // === WHEN (alternative keyword) ===

      hotThreshold <- 30
      when temperature > hotThreshold
        stdout.println("When says: hot day")

Common mistakes

E01072 — EK9 has no return statement. The keyword does not exist in the grammar. Use declared return variables and guard expressions instead. See ek9 -h E01072 for details.

Incorrect:

if temperature > temperatureThreshold
        stdout.println("Hot day")
        return

Correct:

if temperature > temperatureThreshold
        stdout.println("Hot day")
Other ways to ask this
  • How do I write a conditional in EK9?
  • What is the if statement syntax in EK9?
  • How do I test a condition in EK9?

Coming from another language?

Java: if (condition) { } else if { } else { } with parentheses and braces required. Python: if/elif/else with colon and indentation, 'elif' keyword. Rust: if condition { } else if { } else { } with braces required, if is an expression. Go: if condition { } else if { } else { } with braces required, can declare variable in if. Kotlin: if/else is an expression, can return values. C++: if (condition) { } else if { } else { } with parentheses and braces. JavaScript: if (condition) { } else if { } else { } with parentheses and braces. C#: if (condition) { } else if { } else { } with parentheses and braces. Swift: if condition { } else if { } else { } no parentheses, braces required. EK9: if/else with indentation, no parentheses, no braces, no elif (use 'else if'), 'when' as alternative keyword, guard variables for safe null checking.

Keywords: test, conditional, when, comparison, check, if, condition, branch, else, boolean, flow, control