<blockquote>
  <p>Do bitcoin scripts support nested ifs (OP_IF OP_NOTIF inside one another)?</p>
</blockquote>

<p>Yes, Bitcoin support nested IF/ELSE. In fact we use the nested IFs in Lightning Network all the time when adding HTLCs to forward a payment.</p>

<blockquote>
  <p>Is there any limit on how deep a nested if can go?</p>
</blockquote>

<p>There is no explicit rules dictating how far deep the nested IFs can go but <a href="https://github.com/bitcoin/bitcoin/blob/master/src/script/script.h" rel="nofollow noreferrer">there are some constrains</a> on the script itself:</p>

<ul>
<li>Maximum number of bytes pushable to the stack should be less than 520 bytes</li>
<li>Maximum operations per script is limited to 201</li>
<li>Maximum size of the script is 10,000 bytes</li>
<li>Maximum number of values on script interpreter stack is 1,000 bytes</li>
</ul>

<blockquote>
  <p>Is there any examples (raw bytes/hex) I could look at?</p>
</blockquote>

<p>As I mentioned earlier, we use nested IFs all the time while adding HTLCs in the Lightning Network implementation. Below is an output script of <a href="https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#offered-htlc-outputs" rel="nofollow noreferrer">offered HTLC outputs</a> for a peer.</p>

<pre><code># To remote node with revocation key
OP_DUP OP_HASH160 &lt;RIPEMD160(SHA256(revocationpubkey))&gt; OP_EQUAL
OP_IF
    OP_CHECKSIG
OP_ELSE
    &lt;remote_htlcpubkey&gt; OP_SWAP OP_SIZE 32 OP_EQUAL
    OP_NOTIF
        # To local node via HTLC-timeout transaction (timelocked).
        OP_DROP 2 OP_SWAP &lt;local_htlcpubkey&gt; 2 OP_CHECKMULTISIG
    OP_ELSE
        # To remote node with preimage.
        OP_HASH160 &lt;RIPEMD160(payment_hash)&gt; OP_EQUALVERIFY
        OP_CHECKSIG
    OP_ENDIF
OP_ENDIF
</code></pre>

CC BY-SA 4.0
2019-10-08T06:22:50.860Z
`IF/ELSE loops` Is "loop" the correct term to use?
Coding Enthusiast
CC BY-SA 4.0
2019-10-08T07:45:30.757Z
@CodingEnthusiast thanks for correcting. I wrote the answer from my phone.
Ugam Kamat
CC BY-SA 4.0
2019-10-08T05:58:49.880Z
2019-10-08T07:46:29.320Z
2019-10-08T07:46:29.320Z
Ugam Kamat
<p>For example can we convert the following code to a script?  </p>

<pre><code>if(bool_1)
{
    FOO1();
    if(!bool_2)
    {
        FOO2();
    }
    else
    {
        BAR1();
        if(bool_3)
        {
            BAR2();
        }
        BAR3();
    }
}
else
{
    FOOBAR();
}
</code></pre>

<p><code>&lt;push&gt; &lt;push&gt; &lt;push&gt; OP_IF OP_FOO1 OP_NOTIF OP_FOO2 OP_ELSE OP_BAR1 OP_IF OP_BAR2 OP_ENDIF OP_BAR3 OP_ENDIF OP_FOOBAR OP_ENDIF</code>(correct?)  </p>

<p>If yes then:<br>
1. Is there any limit on how deep a nested IFs can go?<br>
2. Is there any examples (raw bytes/hex) I could look at?</p>

<script>
Does Bitcoin script support nested IFs (OP_IF or OP_NOTIF inside one another)?
Coding Enthusiast
# Does Bitcoin script support nested IFs (OP_IF or OP_NOTIF inside one another)?

(Asked by: username_0 on 2019-10-08)

For example can we convert the following code to a script?

```
if(bool_1)
{
    FOO1();
    if(!bool_2)
    {
        FOO2();
    }
    else
    {
        BAR1();
        if(bool_3)
        {
            BAR2();
        }
        BAR3();
    }
}
else
{
    FOOBAR();
}

```

`<push> <push> <push> OP_IF OP_FOO1 OP_NOTIF OP_FOO2 OP_ELSE OP_BAR1 OP_IF OP_BAR2 OP_ENDIF OP_BAR3 OP_ENDIF OP_FOOBAR OP_ENDIF`(correct?)

If yes then:  
1. Is there any limit on how deep a nested IFs can go?  
2. Is there any examples (raw bytes/hex) I could look at?

**Tags:** script


---

## 1 answers

### Answer by username_1 on 2019-10-08. Score: 1 | Accepted answer

> Do bitcoin scripts support nested ifs (OP\_IF OP\_NOTIF inside one another)?

Yes, Bitcoin support nested IF/ELSE. In fact we use the nested IFs in Lightning Network all the time when adding HTLCs to forward a payment.

> Is there any limit on how deep a nested if can go?

There is no explicit rules dictating how far deep the nested IFs can go but [there are some constrains](https://github.com/bitcoin/bitcoin/blob/master/src/script/script.h) on the script itself:

* Maximum number of bytes pushable to the stack should be less than 520 bytes
* Maximum operations per script is limited to 201
* Maximum size of the script is 10,000 bytes
* Maximum number of values on script interpreter stack is 1,000 bytes

> Is there any examples (raw bytes/hex) I could look at?

As I mentioned earlier, we use nested IFs all the time while adding HTLCs in the Lightning Network implementation. Below is an output script of [offered HTLC outputs](https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#offered-htlc-outputs) for a peer.

```
# To remote node with revocation key
OP_DUP OP_HASH160 <RIPEMD160(SHA256(revocationpubkey))> OP_EQUAL
OP_IF
    OP_CHECKSIG
OP_ELSE
    <remote_htlcpubkey> OP_SWAP OP_SIZE 32 OP_EQUAL
    OP_NOTIF
        # To local node via HTLC-timeout transaction (timelocked).
        OP_DROP 2 OP_SWAP <local_htlcpubkey> 2 OP_CHECKMULTISIG
    OP_ELSE
        # To remote node with preimage.
        OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY
        OP_CHECKSIG
    OP_ENDIF
OP_ENDIF

```

> **Comments:**
> **username_0**: `IF/ELSE loops` Is "loop" the correct term to use?  
> **username_1**: @username_0 thanks for correcting. I wrote the answer from my phone.  

---

