OP_IF β
INFO
Opcode number: 99
Byte representation: 0x63
Short description: Pop and execute the next statements if a nonzero element was popped.
OP_IF
is used to perform conditional execution. Scripts in Bitcoin can contain branchesβparts of the script that are executed only if certain conditions are met. The OP_IF
opcode allows the script to evaluate a value and conditionally execute the following script commands based on that value.
Operation β
- Pop the top stack item.
- If the item is not
0
, execute the following opcodes until anOP_ELSE
orOP_ENDIF
is encountered. - If the item is
0
, skip the following opcodes until anOP_ELSE
orOP_ENDIF
is encountered.
You'll see it used like so:
txt
<condition> OP_IF
<script if condition is true>
OP_ELSE
<script if condition is false>
OP_ENDIF
Where <condition>
is some operation or value that will leave a 0
or non-0
value on top of the stack.
Notes β
- Bitcoin script does not support loop structures, so all conditional logic is based on branches.
- The script will fail and not complete if there is no matching
OP_ENDIF
for anOP_IF
.
Examples β
Example 1 β
The following script would leave 2
on the stack as the condition (1
) is true.
shell
# ASM script
OP_1 OP_IF OP_2 OP_ENDIF
# Raw script
51635268
# Final stack
2
Example 2 β
The following script would leave 3
on the stack as the condition (0
) is false.
shell
# ASM script
OP_0 OP_IF OP_2 OP_ELSE OP_3 OP_ENDIF
# Raw script
006352675368
# Final stack
3