Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Control flow

If Expression

The if expression evaluates a condition and executes code based on the result:

 if (x == 1):
   "one"
 elif (x == 2):
   "two"
 else:
   "other"
 if (x == 1):
   do "one" | upcase;
 elif (x == 2):
   do "TWO" | downcase;
 else:
   do
    "other" | upcase
   end
 if (x == 1):
   "one"

The if expression can be nested and chained with elif and else clauses.

Truthiness

A condition is false for false, None, 0, "", [] and {}. Every other value is true, including "0" and [0].

ValueCondition
false, Nonefalse
0false
"", [], {}false
"0", [0], {"a": 1}true

Unless Expression

The unless expression executes code only when a condition is false; the inverse of a single-branch if:

let x = 2 |
unless (x == 1):
  "not one"
# => "not one"

When the condition is true, the body is not evaluated and the result is None:

let x = 1 |
unless (x == 1):
  "not one"
| is_none
# => true

While Expression

The while loop repeatedly executes code while a condition is true:

let x = 5 |
while (x > 0):
  let x = x - 1 | x
end
# => 0

You can use break: <expr> to return a value from a while loop:

var x = 10 |
while (x > 0):
  x = x - 1 |
  if(eq(x, 3)):
    break: "Found three!"
  else:
    x
end
# => "Found three!"

Until Expression

The until loop repeatedly executes code while a condition is false; the inverse of while:

var x = 0 |
until (x >= 5):
  x = x + 1 | x
end
# => 5

Like while, until supports break/break: <expr> and continue inside its body.

Foreach Expression

The foreach loop iterates over elements in an array:

let items = array(1, 2, 3) |
foreach (x, items):
   sub(x, 1)
end
# => [0, 1, 2]

You can use break: <expr> to exit early and return a specific value instead of an array:

let items = array(1, 2, 3, 4, 5) |
foreach (x, items):
  if(x > 3):
    break: "Found value greater than 3"
  else:
    x
end
# => "Found value greater than 3"

Foreach loops are useful for:

  • Processing arrays element by element
  • Mapping operations across collections
  • Filtering and transforming data

Loop Expression

The loop expression creates an infinite loop that continues until explicitly terminated with break:

var x = 0 |
loop:
  x = x + 1 |
  if(x > 5):
    break
  else:
    x
end
# => 5

The loop can be controlled using break to exit the loop and continue to skip to the next iteration:

var x = 0 |
loop:
  x = x + 1 |
  if(x < 3):
    continue
  elif(x > 5):
    break
  else:
    x
end
# => 5

The break statement can return a value from a loop using the break: <expr> syntax. This allows loops to be used as expressions that produce a specific value when exited:

var x = 0 |
loop:
  x = x + 1 |
  if(x > 5):
    break: "Found it!"
  else:
    x
end
# => "Found it!"

Loop expressions are useful for:

  • Implementing infinite loops with conditional exits
  • Creating retry mechanisms
  • Processing until a specific condition is met
  • Complex iteration patterns that don’t fit while or foreach