OP_ELSE
INFO
Opcode number: 103
Byte representation: 0x67
Short description: Execute following script if the previous OP_IF or OP_NOTIF condition was not met.
OP_ELSE is an essential part of conditional structures in Bitcoin scripting. It allows for alternative execution paths depending on conditions evaluated by preceding OP_IF or OP_NOTIF opcodes.
Operation
- If the preceding
OP_IForOP_NOTIFcondition was met (and its script executed), skip the opcodes followingOP_ELSEuntil anOP_ENDIFis encountered. - If the preceding condition was not met, execute the opcodes after
OP_ELSEuntil anOP_ENDIFis found.
The structure generally appears like so:
txt
<condition> OP_IF
<script if condition is true>
OP_ELSE
<script if condition is false>
OP_ENDIFOr, in conjunction with OP_NOTIF:
txt
<condition> OP_NOTIF
<script if condition is false>
OP_ELSE
<script if condition is true>
OP_ENDIFWhere <condition> is an operation or value that results in a 0 or non-0 value on top of the stack.
Notes
OP_ELSEis not standalone and must be used within anOP_IF/OP_NOTIFandOP_ENDIFstructure.- A script will fail and not complete if there is no matching
OP_ENDIFfor anOP_ELSE.
Examples
Example 1
The following script leaves 2 on the stack since the condition (1) is true in front of an OP_IF`.
shell
# ASM script
OP_1 OP_IF OP_2 OP_ELSE OP_3 OP_ENDIF
# Raw script
516352675368
# Final stack
2Example 2
The following script leaves 3 on the stack since the condition (1) is true in front of an OP_NOTIF.
shell
# ASM script
OP_1 OP_NOTIF OP_2 OP_ELSE OP_3 OP_ENDIF
# Raw script
516452675368
# Final stack
3