(if...)
Create an if...then...else block. The instruction will pop a i32 value of the stack. If the value is not a zero then the then code block section is performed. But, if the value is zero, then the else section is processed, if it exists.

Syntax

if
    ...
end
if
    ...
else
    ...
end
(if 
    (then
       ...
    )
)
(if 
    (then
        ...
    )
    (else
        ...
    )
)
(if
    (
        ;; condition
    )
    (then
        ...
    )
    (else
        ...
    )
)
if (param...) (result...)
...
else
...
end

Parameters

(param...) Optional
Lists all the values on the stack that will be used inside the code block. This can only be used if there is an else section. Both sections need to pop the same number of values off the stack.
(result...) Optional
Lists all the values that will be pushed onto the stack when the execution moves out of the code block. This can only be used if there is an else section. Both sections need to push the same number of values onto the stack.

Stack In

i32 The control condition value.

Stack Out

NONE

Examples

;; Push 1 onto stack
i32.const 1
if
  ;; Because the stack had a non-zero value
  ;; this part will be processed.
else
  ;; But, if it was a zero then this part
  ;; would have been processed.
end
;; Add value to stack
i32.const 42

;; Perform condition
i32.const 1
if (param i32) (result i32)
  ;; Add 10 to parameter and push result onto stack
  i32.const 10
  i32.add
else
  ;; Add 20 to parameter and push result onto stack
  i32.const 20
  i32.add
end