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.
1Your 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 – 2019-10-03T11:17:42.680