If/Else & select
if is the most readable way to branch. It pops an i32 condition and runs the then part if it is non-zero,
otherwise the else part (if any).
Folded and flat syntax
In folded form the condition comes first, followed by (then …) and an optional (else …):
(module
(import "console" "log" (func $log (param i32)))
(func $check_age (param $age i32)
(if (i32.ge_u (local.get $age) (i32.const 18))
(then (call $log (i32.const 1))) ;; adult
(else (call $log (i32.const 0))))) ;; minor
(func (export "main")
(call $check_age (i32.const 21))
(call $check_age (i32.const 15))))1
0
The same thing in flat form: push the condition, then if … else … end:
(module
(import "console" "log" (func $log (param i32)))
(func $check_age (param $age i32)
local.get $age
i32.const 18
i32.ge_u
if
i32.const 1
call $log
else
i32.const 0
call $log
end)
(func (export "main")
(call $check_age (i32.const 21))
(call $check_age (i32.const 15))))1
0
if as an expression
Give the if a result type and both branches must produce a value of that type. Then it works like the ternary
operator cond ? a : b:
(module
(import "console" "log" (func $log (param i32)))
;; shipping cost in cents: free from 5000 cents, otherwise 499
(func $shipping (param $total i32) (result i32)
(if (result i32) (i32.ge_u (local.get $total) (i32.const 5000))
(then (i32.const 0))
(else (i32.const 499))))
(func (export "main")
(call $log (call $shipping (i32.const 7250)))
(call $log (call $shipping (i32.const 1999)))))0
499
An if with a result must have an else; otherwise there would be no value when the condition is false:
(module
(func (export "main") (result i32)
(if (result i32) (i32.const 1)
(then (i32.const 10)))))main.wat:4:27: error: type mismatch in `if false` branch, expected [i32] but got []
(then (i32.const 10)))))
^
else-if chains
There is no elif: nest another if inside the else. Here is a letter grade calculator returning ASCII codes:
(module
(import "console" "str" (func $print (param i32 i32)))
(memory (export "memory") 1)
(func $grade (param $score i32) (result i32)
(if (result i32) (i32.ge_s (local.get $score) (i32.const 90))
(then (i32.const 65)) ;; 'A'
(else
(if (result i32) (i32.ge_s (local.get $score) (i32.const 80))
(then (i32.const 66)) ;; 'B'
(else
(if (result i32) (i32.ge_s (local.get $score) (i32.const 70))
(then (i32.const 67)) ;; 'C'
(else (i32.const 70)))))))) ;; 'F'
(func $show (param $score i32)
(i32.store8 (i32.const 0) (call $grade (local.get $score))) ;; write the letter to address 0
(call $print (i32.const 0) (i32.const 1)))
(func (export "main")
(call $show (i32.const 95))
(call $show (i32.const 83))
(call $show (i32.const 71))
(call $show (i32.const 40))))A
B
C
F
i32.store8 writes one byte to memory, which console.str then prints as a character.
Branching out of an if
An if is also a block, so br inside it can target the if (jumping to its end) or any enclosing block.
br 0 inside a then exits the if:
(module
(import "console" "log" (func $log (param i32)))
(func (export "main")
(if (i32.const 1)
(then
(call $log (i32.const 1))
(br 0) ;; leave the if
(call $log (i32.const 2))))
(call $log (i32.const 3))))1
3
select vs if
select evaluates both values and then chooses one. if evaluates only the branch it takes.
select |
if … else |
|
|---|---|---|
| Evaluates | both operands, always | only the taken branch |
| Branching | none (compiles to a conditional move) | a real jump |
| Good for | cheap values: min, max, abs |
expensive or side-effecting work |
(module
(import "console" "log" (func $log (param i32)))
(func $noisy (param $v i32) (result i32)
(call $log (local.get $v)) ;; side effect: shows when it runs
(local.get $v))
(func (export "main")
;; select: both noisy calls run, then 10 is chosen
(call $log (select (call $noisy (i32.const 10)) (call $noisy (i32.const 20)) (i32.const 1)))
;; if: only the taken branch runs
(call $log
(if (result i32) (i32.const 1)
(then (call $noisy (i32.const 30)))
(else (call $noisy (i32.const 40)))))))10
20
10
30
30
The select line prints 10, 20 (both calls) and then the chosen 10; the if prints only 30 twice
(once inside $noisy, once as the result).
Tip:
selectcan take an explicit type,(select (result f64) …), and needs one when the operands are reference types.
Test yourself
When must an if have an else branch?
- Always
- When it has a result type
- When the condition is a comparison
- Never
How do you write "else if" in WAT?
elif(else-if …)- Nest another
ifinside theelse - Use
br_ifonly
Which statement about select is true?
- It evaluates only the chosen operand
- It evaluates both operands before choosing
- It can only choose between constants
- It requires an
else