Script - OP_IF etc. clarifications

7

2

Description of the OP IF is a bit vague on the wiki. Where does one get statements for the op for? Are they coming from the stack, or from somewhere else? Are there any available examples of how a script using that op code looks like?

ThePiachu

Posted 2011-12-27T00:50:04.577

Reputation: 41 594

Answers

10

The statements are not on the stack, they're in the script. They start immediately after the OP_IF. They end at the first OP_ELSE or OP_ENDIF, assuming there isn't a nested OP_IF or OP_NOTIF. So it looks like this

// script to put a number on the stack
OP_IF
// script that runs only if the number isn't zero
OP_ENDIF
// script that runs no matter what

or:

// script to put a number on the stack
OP_IF
// script that runs only if the number isn't zero
OP_ELSE
// script that runs only if the number is zero
OP_ENDIF
// script that runs no matter what

You can read a bit more about in this chat transcript.

David Schwartz

Posted 2011-12-27T00:50:04.577

Reputation: 46 931