Booleans and Comparisons
Demonstrates boolean values and comparison operators.
Example 009 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.
How it is built
The cell Booleans has no face and no memory. Handlers: run (line 19).
The source
009_booleans.cell
// ============================================================================
// 009 — Booleans and Comparisons
// ============================================================================
// Demonstrates boolean values and comparison operators.
//
// Run: soma run 009_booleans.cell run
// Expect:
// true is Bool
// 10 > 5: true
// 10 == 10: true
// 10 != 5: true
// 10 >= 10: true
// 10 <= 15: true
// both true: yes
// either true: yes
cell Booleans {
on run() {
// Boolean literals
let t = true
let f = false
let t_type = type_of(t)
print("{t} is {t_type}")
// Comparison operators return booleans
let gt = 10 > 5
print("10 > 5: {gt}")
let eq = 10 == 10
print("10 == 10: {eq}")
let ne = 10 != 5
print("10 != 5: {ne}")
let ge = 10 >= 10
print("10 >= 10: {ge}")
let le = 10 <= 15
print("10 <= 15: {le}")
// Boolean logic with && and || operators
let a = true
let b = true
if a && b {
print("both true: yes")
}
// OR: check either condition
let x = false
let y = true
if x || y {
print("either true: yes")
}
return "done"
}
}What the toolchain says
$ soma check 009_booleans.cell
✓ All checks passed.$ soma run 009_booleans.cell run
true is Bool
10 > 5: true
10 == 10: true
10 != 5: true
10 >= 10: true
10 <= 15: true
both true: yes
either true: yes
done