# How many IF/ELSE/ENDIF can be used in a script?

Besides the 520 bytes consensus rule limit, is there a limit on how many of each OP_IF, OP_ELSE and OP_ENDIF can be used in a single script?

For example, a company that has 3 people in its purchase department would like to make a fast purchase if all people in the department agree and slow it down in case not everybody agrees, but not make it impossible in the final:

• Alice, Bob and Carol store funds in a P2SH address. Initially, all 3/3 of signatures are required initially.

• After some time, (OP_CHECKLOCKTIMEVERIFY) just two of them can spend (2/3 signatures required).

• After more time, (first OP_CHECKLOCKTIMEVERIFY + more time), any one can spend (1/3 signatures required).

How would a script like this look like?

• I don't think there's any rule on how many you can have. They do need to be matched, though. Commented Jul 22, 2017 at 22:03
• there is limit of 201 total operations in script as far as I remember. Commented Jul 22, 2017 at 22:38

``````OP_IF
OP_3
OP_ELSE
OP_IF
lock1 OP_CLTV OP_DROP OP_2
OP_ELSE
lock2 OP_CLTV OP_DROP OP_1
OP_ENDIF
OP_ENDIF
pub1 pub2 pub3 OP_3 OP_CHECKMULTISIG
``````

some pseudocode:

``````var x = stack.pop ( );  // take the last item from stack and remove it
if ( x != 0 )
{
stack.push ( 3 );  // push the number '3' to the stack
}
else
{
var y = stack.pop ( ); // take next item
if ( y != 0 )
{
stack.push ( locktime1 ); // push value
stack.compare_top_value_with_timestamp_and_abort_if_not_valid ( );
stack.pop ( ); // remove locktime1 if passed
stack.push ( 2 );
}
else
{
stack.push ( locktime2 ); // push value
stack.compare_top_value_with_timestamp_and_abort_if_not_valid ( );
stack.pop ( ); // remove locktime2 if passed
stack.push ( 1 );
}
}
stack.push ( pub1 );
stack.push ( pub2 );
stack.push ( pub3 );
stack.push ( 3 );
stack.check_multi_sig ( );
``````
• Thank you very much may I ask why you start with OP_IF and not OP_3 and put the pub keys at the end pf the script? I just want to underastand not mechanically copy paste. Some // comments on steps would be useful. Commented Jul 22, 2017 at 22:18
• Answer updated. I can not explain the things better. You have to understand how m-of-n multisig scripts work Commented Jul 22, 2017 at 22:52
• If i would like to state exactly which two pubkeys cand spend can this be done? For example after lock1 expires pub1 and pub3 can spend and after lock2 just pub2? Commented Jul 23, 2017 at 15:50
• @skydanc3r, my script works as described in starting topic. Any two after locktime1 or even one of them after locktime2. Of course, it is possible to imagine another logic and create another script Commented Jul 23, 2017 at 15:55
• asked again in a different question with full description in topic. bitcoin.stackexchange.com/questions/57005/… Commented Jul 23, 2017 at 17:27