TERM_DEF // MODULE_4_BRANCHING / OP_IF
OP_IF
OP_IF. Begin a branch executed only if the top item is truthy.
OP_IF pops the top stack item. If it's truthy, the opcodes between OP_IF and OP_ELSE (or OP_ENDIF if there's no OP_ELSE) are executed. If falsy, they are skipped. The condition value is consumed — it doesn't remain on the stack. OP_NOTIF is the inverse: its block runs when the condition is falsy.
This page sits in the Module 4 — Branching section — Vocabulary introduced in the Branching module. Read on for what it is, why it exists, how it works under the hood, and what to watch out for.
OP_IF pops the top stack item. If it's truthy, the opcodes between OP_IF and OP_ELSE (or OP_ENDIF if there's no OP_ELSE) are executed. If falsy, they are skipped. The condition value is consumed — it doesn't remain on the stack. OP_NOTIF is the inverse: its block runs when the condition is falsy.
This page sits in the Module 4 — Branching section — Vocabulary introduced in the Branching module. Read on for what it is, why it exists, how it works under the hood, and what to watch out for.
WHAT_OP_IF_IS
OP_IF — at a glance
MODULE 4
OP_IF is a Bitcoin Script opcode in the FLOW family. Its stack effect is
cond →. Pop top; if truthy, execute up to matching ELSE/ENDIF. Begin a branch executed only if the top item is truthy.OP_IF pops the top stack item. If it's truthy, the opcodes between OP_IF and OP_ELSE (or OP_ENDIF if there's no OP_ELSE) are executed. If falsy, they are skipped. The condition value is consumed — it doesn't remain on the stack. OP_NOTIF is the inverse: its block runs when the condition is falsy.
Why it exists
DESIGN
HOW_IT_WORKS
Mechanism
HOW IT WORKS
Every UTXO is locked by a scriptPubKey — the output's locking script. To spend it, you provide a scriptSig (or witness) containing data that satisfies the lock. The node concatenates them, runs the combined script on a stack machine, and accepts the spend if and only if execution finishes with a single truthy value on the stack. OP_IF contributes a specific stack effect within that process — opcodes either push, pop, copy, hash, branch, or verify, and they do so left-to-right deterministically.
1. The script is parsed into a sequence of opcodes and push-data items.
2. Execution starts with an empty stack and an empty alt-stack.
3. Each opcode runs in order — push opcodes add to the stack, others consume the top items and may push results.
4. When OP_IF is reached, it performs its specific stack effect (see below).
5. Final state: a single non-zero (truthy) value on top → the spend is authorised. Anything else (empty stack, false, error) → the script fails and the tx is rejected.
WORKED_EXAMPLE
OP_IF — stack effect + canonical use
EXAMPLE
Opcode : OP_IF
Family : FLOW
Stack effect: cond →
Behaviour : Pop top; if truthy, execute up to matching ELSE/ENDIF.
Open /playground in another tab and search for OP_IF.
Drag the opcode in, watch the stack visualisation step through it
against any combination of inputs you choose.
KEY_PROPERTIES
FAMILY
Belongs to the FLOW family of Script opcodes — siblings share validation rules and historical evolution.
STACK EFFECT
Exactly cond →. Every full node enforces the same effect, byte-for-byte.
ACTIVE
Active on mainnet today; every spend that uses it is being validated by every full node.
COMMON_PITFALLS
Things that catch people out
PITFALLS
- Opcode semantics are consensus — verify against the latest Bitcoin Core source before relying on any subtle behaviour.
RELATED_CONCEPTS
Other terms from Module 4 — Branching — click any to read its page:
TERMINOLOGY_INDEX
TERMINOLOGY
OP_IF
Begin a branch executed only if the top item is truthy.
Branching
Conditional execution controlled by OP_IF / OP_ELSE / OP_ENDIF.
conditionStack
Internal stack that tracks which branch is currently executing.