Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In bitcoin, you can create a script like this:


But could you place another if statement inside the first?


I grant that this is a bit of a silly example (given that the whole thing could be replaced with OP_RETURN) but is that a valid script?

share|improve this question
up vote 2 down vote accepted

Firstly, I'd like to note that you could change your script to OP_1 OP_1 OP_BOOLAND OP_IF OP_RETURN OP_ENDIF and it would be the same.

I'm reading the bitcoin source code, and I can't see a check for nested OP_IF's. These are the IF related checks I've found:

  • No IF/NOTIF without a value on the stack
  • No ELSE/ENDIF without an IF/NOTIF
  • No IF/NOTIF without an ENDIF

So it would let you nest IF's. In fact, it appears that allowing people to nest IF's is intended. Take this variable, which is used to figure out whether a branch of an IF is executing:

vector<bool> vfExec;

If they didn't want nested IF's, they could have made that a single bool variable.

Amusing fact that I figured out while researching this: The script OP_1 OP_IF OP_ELSE OP_ELSE OP_ENDIF is entirely valid.

share|improve this answer
Indeed, you can have multiple OP_ELSE's. Each one inverts the meaning, so <a IF b ELSE c ELSE d ENDIF e> is equivalent to <a IF b d ELSE c ENDIF e>. – Pieter Wuille Jan 26 '14 at 11:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.