<p>Bitcoin uses a stack based language due to which the logical condition that will execute the IF or ELSE statements comes prior to the operator IF or ELSE. Let's take both your cases one by one and see how it gets executed on the stack.</p>

<p><strong>Case 1:</strong> <code>ScriptSig: 0 &lt;Alice's signature&gt; &lt;Bob's signature&gt; 0</code></p>

<ul>
<li>The stack starts at <code>0 &lt;Alice's signature&gt; &lt;Bob's signature&gt; 0</code></li>
<li>Since 0 is on top of the stack, the ELSE condition will execute and drop the 0</li>
<li>number 2 is pushed on the stack</li>
<li>Stack is now <code>0 &lt;Alice's signature&gt; &lt;Bob's signature&gt; 2</code></li>
<li>script outside the IF/ELSE statement is pushed to the stack</li>
<li>Stack is now <code>0 &lt;Alice's signature&gt; &lt;Bob's signature&gt; 2 &lt;Alice's pubkey&gt; &lt;Bob's pubkey&gt; 2 CHECKMULTISIG</code></li>
<li>This is a normal 2-of-2 Multi-sig operation that will execute by verifying both Alice's and Bob's signature to the public keys</li>
</ul>

<p><strong>Case 2:</strong> <code>ScriptSig: 0 &lt;Alice/Bob's signature&gt; &lt;Lenny's signature&gt; 1</code></p>

<ul>
<li><p>The stack starts at <code>0 &lt;Alice/Bob's signature&gt; &lt;Lenny's signature&gt; 1</code></p></li>
<li><p>Since 1 is on top of the stack, the IF statement will execute and drop the 1</p></li>
<li><p><code>&lt;now + 3 months&gt; CHECKLOCKTIMEVERIFY</code> is pushed to the stack and verified whether 3 months have passed. If yes, the execution will continue. </p></li>
<li><p><code>DROP</code> operator drops the TRUE on top of the stack if verification is successful</p></li>
<li><p><code>&lt;Lenny's pubkey&gt; CHECKSIGVERIFY</code> is pushed to the stack</p></li>
<li><p>Stack is now <code>0 &lt;Alice/Bob's signature&gt; &lt;Lenny's signature&gt; &lt;Lenny's pubkey&gt; CHECKSIGVERIFY</code></p></li>
<li><p><code>CHECKSIGVERIFY</code> will consume the top 2 items of the stack. It will check whether Lenny's signature matches Lenny's pubkey. If yes, the execution will continue.</p></li>
<li>Stack is now <code>0 &lt;Alice/Bob's signature&gt;</code></li>
<li>1 is pushed to the stack</li>
<li>Stack is now <code>0 &lt;Alice/Bob's signature&gt; 1</code></li>
<li>Script outside the IF/ELSE statement is pushed to the stack</li>
<li>Stack is now <code>0 &lt;Alice/Bob's signature&gt; 1 &lt;Alice's pubkey&gt; &lt;Bob's pubkey&gt; 2 CHECKMULTISIG</code></li>
<li>This is now a normal 1-of-2 multisig operation and will execute if Alice or Bob provides a valid signatures to their pub key.</li>
</ul>

CC BY-SA 4.0
2019-10-03T11:16:26.480Z
2019-10-10T08:50:46.660Z
2019-10-10T08:50:46.660Z
Ugam Kamat
<p>I'm quoting this <a href="https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki" rel="nofollow noreferrer">example of BIP65</a></p>

<p>BIP 65 has this bitcoin script code</p>

<pre><code> IF
    &lt;now + 3 months&gt; CHECKLOCKTIMEVERIFY DROP
    &lt;Lenny's pubkey&gt; CHECKSIGVERIFY
    1
 ELSE
    2
 ENDIF
 &lt;Alice's pubkey&gt; &lt;Bob's pubkey&gt; 2 CHECKMULTISIG
</code></pre>

<p>And with this bitcoin scriptSig is possible unlock the transaction</p>

<ul>
<li>Now with this ScriptSig <code>0 &lt;Alice's signature&gt; &lt;Bob's signature&gt; 0</code></li>
<li>after 3 months with this ScirpSig <code>0 &lt;Alice/Bob's signature&gt; &lt;Lenny's signature&gt; 1</code></li>
</ul>

<p>But I don't understand this part of If-then-else</p>

<pre><code>ELSE
    2
</code></pre>

<p>When is it executed and what I don't understand inside the if-then-else condition?</p>

CC BY-SA 4.0
2019-10-03T11:17:42.680Z
Your ELSE loop was indented inside the IF condition, which is not the case in BIP 65. I have edited it to reflect the correct script since you have quoted that example.
Ugam Kamat
<bitcoin-core><script><checklocktimeverify>
Clarification on execution of complex script with OP_CHECKLOCKTIMEVERIFY
vincenzopalazzo
# Clarification on execution of complex script with OP_CHECKLOCKTIMEVERIFY

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

I'm quoting this [example of BIP65](https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki)

BIP 65 has this bitcoin script code

```
 IF
    <now + 3 months> CHECKLOCKTIMEVERIFY DROP
    <Lenny's pubkey> CHECKSIGVERIFY
    1
 ELSE
    2
 ENDIF
 <Alice's pubkey> <Bob's pubkey> 2 CHECKMULTISIG

```

And with this bitcoin scriptSig is possible unlock the transaction

* Now with this ScriptSig `0 <Alice's signature> <Bob's signature> 0`
* after 3 months with this ScirpSig `0 <Alice/Bob's signature> <Lenny's signature> 1`

But I don't understand this part of If-then-else

```
ELSE
    2

```

When is it executed and what I don't understand inside the if-then-else condition?

**Tags:** bitcoin-core, script, checklocktimeverify

> **Comments:**
> **username_1**: Your ELSE loop was indented inside the IF condition, which is not the case in BIP 65. I have edited it to reflect the correct script since you have quoted that example.  

---

## 1 answers

### Answer by username_1 on 2019-10-03. Score: 4 | Accepted answer

Bitcoin uses a stack based language due to which the logical condition that will execute the IF or ELSE statements comes prior to the operator IF or ELSE. Let's take both your cases one by one and see how it gets executed on the stack.

**Case 1:** `ScriptSig: 0 <Alice's signature> <Bob's signature> 0`

* The stack starts at `0 <Alice's signature> <Bob's signature> 0`
* Since 0 is on top of the stack, the ELSE condition will execute and drop the 0
* number 2 is pushed on the stack
* Stack is now `0 <Alice's signature> <Bob's signature> 2`
* script outside the IF/ELSE statement is pushed to the stack
* Stack is now `0 <Alice's signature> <Bob's signature> 2 <Alice's pubkey> <Bob's pubkey> 2 CHECKMULTISIG`
* This is a normal 2-of-2 Multi-sig operation that will execute by verifying both Alice's and Bob's signature to the public keys

**Case 2:** `ScriptSig: 0 <Alice/Bob's signature> <Lenny's signature> 1`

* The stack starts at `0 <Alice/Bob's signature> <Lenny's signature> 1`
* Since 1 is on top of the stack, the IF statement will execute and drop the 1
* `<now + 3 months> CHECKLOCKTIMEVERIFY` is pushed to the stack and verified whether 3 months have passed. If yes, the execution will continue.
* `DROP` operator drops the TRUE on top of the stack if verification is successful
* `<Lenny's pubkey> CHECKSIGVERIFY` is pushed to the stack
* Stack is now `0 <Alice/Bob's signature> <Lenny's signature> <Lenny's pubkey> CHECKSIGVERIFY`
* `CHECKSIGVERIFY` will consume the top 2 items of the stack. It will check whether Lenny's signature matches Lenny's pubkey. If yes, the execution will continue.
* Stack is now `0 <Alice/Bob's signature>`
* 1 is pushed to the stack
* Stack is now `0 <Alice/Bob's signature> 1`
* Script outside the IF/ELSE statement is pushed to the stack
* Stack is now `0 <Alice/Bob's signature> 1 <Alice's pubkey> <Bob's pubkey> 2 CHECKMULTISIG`
* This is now a normal 1-of-2 multisig operation and will execute if Alice or Bob provides a valid signatures to their pub key.


---

