Skip to content

04 · Operators

Chained comparisons

Write lower <= value < upper as one comparison chain. Each shared operand is evaluated once, and a failed comparison stops the chain. This keeps interval checks readable without repeating a potentially expensive expression.

On this page

Write lower <= value < upper as one comparison chain. Each shared operand is evaluated once, and a failed comparison stops the chain. This keeps interval checks readable without repeating a potentially expensive expression.

Complete example

Chained comparisons.

15-chained-comparisons.zolo
Playground
// Chained comparisons reuse the middle value and stop on the first false pair.

var calls = 0
fn read(value: int) -> int { calls += 1; value }

let age = 18
print(0 <= age < 150) // expected: true

print(1 < 2 <= 2) // expected: true

print(4 > 2 >= 2) // expected: true

print(0 < read(2) < 3) // expected: true

print(calls) // expected: 1

calls = 0
print(5 < read(2) < read(9)) // expected: false

print(calls) // expected: 1

print(1d < 2d <= 3d) // expected: true

print("a" < "b" < "c") // expected: true

Rules and behavior

Conditions and error recovery · Documentation

From a checkout of the repository:

zolo examples/features/03-operators/15-chained-comparisons.zolo

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br