<p>It duplicates the top stack item if the top stack item is not 0 and not negative 0. I'm not sure where this would be useful.</p>

CC BY-SA 3.0
2012-03-19T21:46:15.307Z
Thanks.  I fixed the wiki accordingly.  I copied the description from the OP_IF operation, since the code is the same.
Chris Moore
CC BY-SA 3.0
2012-03-19T21:27:50.993Z
2012-03-19T21:27:50.993Z
theymos
<p>Another use said there is not a well known use, but I think that OP_IFDUP is super nifty. Adding my response (years later) to share the knowledge in case someone else comes across this post.</p>

<p>Consider the following script:</p>

<pre><code>OP_DUP
OP_0
&lt;2&gt;
OP_WITHIN
OP_VERIFY

OP_IFDUP
OP_NOTIF
... sub program 0
OP_0
OP_ENDIF
OP_1SUB

OP_IFDUP
OP_NOTIF
... sub program 1
OP_0
OP_ENDIF
OP_1SUB

OP_IFDUP
OP_NOTIF
... sub program 2
OP_ENDIF
OP_1
</code></pre>

<p>This program emulates a switch!</p>

<pre><code>switch(x) {
case 0:
   // program 0
case 1:
  // program 1
case 2:
 // program 2
default:
  assert(0);
}
</code></pre>

<p>But is also slightly more powerful because the last OP_0 per branch can specify if any future branches should also execute (can be used for fall-through behavior, if there is a logical clause common to a group!).</p>

<p>Compare to the more "straightforward" solutions using OP_IF and OP_EQUAL, there's a few more opcodes per branch, and more overall when n = 3. Further the need to push the number on the stack in the code can contribute more bytes for even bigger switches.</p>

<pre><code>OP_DUP
&lt;0&gt;
OP_EQUAL
OP_IF
OP_DROP
... sub program 0
OP_0
OP_ENDIF

OP_DUP
&lt;1&gt;
OP_EQUAL
OP_IF
OP_DROP
... sub program 1
OP_0
OP_ENDIF

OP_DUP
&lt;2&gt;    
OP_EQUAL
OP_IF
OP_DROP
... sub program 2
OP_ENDIF
OP_1
</code></pre>

CC BY-SA 4.0
2020-04-17T08:07:17.697Z
2020-04-17T08:07:17.697Z
Jeremy Rubin
<p>According to <a href="https://en.bitcoin.it/wiki/Script#Stack" rel="nofollow">the wiki page</a>, <code>OP_IFDUP</code> will duplicate the top of the stack "if the input is true or false".  It seems to me that everything is true or false when interpreted as a boolean.</p>

<p>According to the source code:</p>

<pre><code>case OP_IFDUP:
    [...]
    valtype vch = stacktop(-1);
    if (CastToBool(vch))
        stack.push_back(vch);
</code></pre>

<p>That suggests the wiki is incorrect.  What would be a better way of wording it, and what is the operation for?</p>

<script><bitcoin-wiki>
What does script operation "OP_IFDUP" do?
Chris Moore
# What does script operation "OP_IFDUP" do?

(Asked by: username_0 on 2012-03-19)

According to [the wiki page](https://en.bitcoin.it/wiki/Script#Stack), `OP_IFDUP` will duplicate the top of the stack "if the input is true or false". It seems to me that everything is true or false when interpreted as a boolean.

According to the source code:

```
case OP_IFDUP:
    [...]
    valtype vch = stacktop(-1);
    if (CastToBool(vch))
        stack.push_back(vch);

```

That suggests the wiki is incorrect. What would be a better way of wording it, and what is the operation for?

**Tags:** script, bitcoin-wiki


---

## 2 answers

### Answer by username_1 on 2012-03-19. Score: 2 | Accepted answer

It duplicates the top stack item if the top stack item is not 0 and not negative 0. I'm not sure where this would be useful.

> **Comments:**
> **username_0**: Thanks.  I fixed the wiki accordingly.  I copied the description from the OP_IF operation, since the code is the same.  

---

### Answer by username_2 on 2020-04-17. Score: 1

Another use said there is not a well known use, but I think that OP\_IFDUP is super nifty. Adding my response (years later) to share the knowledge in case someone else comes across this post.

Consider the following script:

```
OP_DUP
OP_0
<2>
OP_WITHIN
OP_VERIFY

OP_IFDUP
OP_NOTIF
... sub program 0
OP_0
OP_ENDIF
OP_1SUB

OP_IFDUP
OP_NOTIF
... sub program 1
OP_0
OP_ENDIF
OP_1SUB

OP_IFDUP
OP_NOTIF
... sub program 2
OP_ENDIF
OP_1

```

This program emulates a switch!

```
switch(x) {
case 0:
   // program 0
case 1:
  // program 1
case 2:
 // program 2
default:
  assert(0);
}

```

But is also slightly more powerful because the last OP\_0 per branch can specify if any future branches should also execute (can be used for fall-through behavior, if there is a logical clause common to a group!).

Compare to the more "straightforward" solutions using OP\_IF and OP\_EQUAL, there's a few more opcodes per branch, and more overall when n = 3. Further the need to push the number on the stack in the code can contribute more bytes for even bigger switches.

```
OP_DUP
<0>
OP_EQUAL
OP_IF
OP_DROP
... sub program 0
OP_0
OP_ENDIF

OP_DUP
<1>
OP_EQUAL
OP_IF
OP_DROP
... sub program 1
OP_0
OP_ENDIF

OP_DUP
<2>    
OP_EQUAL
OP_IF
OP_DROP
... sub program 2
OP_ENDIF
OP_1

```


---

