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

<p>I'm reading the <a href="https://github.com/bitcoin/bitcoin/blob/master/src/script.cpp#L407" rel="noreferrer">bitcoin source code</a>, and I can't see a check for nested OP_IF's. These are the IF related checks I've found:</p>

<ul>
<li>No IF/NOTIF without a value on the stack</li>
<li>No ELSE/ENDIF without an IF/NOTIF</li>
<li>No IF/NOTIF without an ENDIF</li>
</ul>

<p>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:</p>

<pre><code>vector&lt;bool&gt; vfExec;
</code></pre>

<p>If they didn't want nested IF's, they could have made that a single <code>bool</code> variable.</p>

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

CC BY-SA 3.0
2014-01-26T11:00:45.420Z
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
CC BY-SA 3.0
2013-02-26T21:40:47.827Z
2013-02-26T21:40:47.827Z
Nick ODell
<p>In bitcoin, you can create a script like this:</p>

<pre><code>OP_1 OP_IF OP_RETURN OP_ENDIF
</code></pre>

<p>But could you place another if statement inside the first?</p>

<pre><code>OP_1 OP_IF OP_1 OP_IF OP_RETURN OP_ENDIF OP_ENDIF
</code></pre>

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

<script>
In Bitcoin scripting, can you have an IF within an IF?
Nick ODell
# In Bitcoin scripting, can you have an IF within an IF?

(Asked by: username_0 on 2013-02-26)

In bitcoin, you can create a script like this:

```
OP_1 OP_IF OP_RETURN OP_ENDIF

```

But could you place another if statement inside the first?

```
OP_1 OP_IF OP_1 OP_IF OP_RETURN OP_ENDIF OP_ENDIF

```

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?

**Tags:** script


---

## 1 answers

### Answer by username_0 on 2013-02-26. Score: 5 | Accepted answer

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](https://github.com/bitcoin/bitcoin/blob/master/src/script.cpp#L407), 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.

> **Comments:**
> **username_1**: 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>.  

---

