user659: According to the wiki page, 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?

user101850: 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


user403: 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.

659: <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>
 101850: <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>
 403: <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>
