Definition
OP_IF and its companions OP_NOTIF, OP_ELSE, and OP_ENDIF are the conditional flow-control opcodes of Bitcoin Script. They let a single locking script contain more than one spending path, with the branch taken decided at spend time by a value the spender places on the stack.
How conditionals execute
OP_IF pops the top stack item: if it is true (non-zero), the statements up to the matching OP_ELSE or OP_ENDIF execute; if false, they are skipped and any OP_ELSE branch runs instead. OP_NOTIF is the inverse. Every OP_IF must be closed by an OP_ENDIF or the script is invalid. Branches can be nested, and the spender steers execution by pushing the appropriate selector before the conditional — for example a 1 to take the “happy path” or a 0 to take a timeout-refund branch.
Why it matters for contracts
Conditionals are what make a single output support several mutually exclusive outcomes. A classic pattern combines OP_IF with a timelock: one branch lets the recipient claim funds with a signature, while the OP_ELSE branch lets the sender reclaim them after a deadline. Hash Time-Locked Contracts, atomic swaps, and many Lightning scripts are built this way. Tapscript later allowed some of these branches to be split into separate, individually revealed leaves, but OP_IF remains the canonical inline branching tool.
These opcodes operate directly on the Script Stack and are routinely paired with timelock opcodes such as OP_CHECKLOCKTIMEVERIFY (CLTV) to build refundable contracts.
In Simple Terms
OP_IF and its companions OP_NOTIF, OP_ELSE, and OP_ENDIF are the conditional flow-control opcodes of Bitcoin Script. They let a single locking script contain more than…
