?dup

(thing) by ariels Sat May 06 2000 at 14:08:06

(Forth:)

If the top word on the stack is non-zero (i.e. "true") then duplicate it. More massively-useful than it would appear, since it allows stuff like:

: countdown ( n -- )
    begin ?dup while . 1- repeat cr ;
to print the numbers from n down; here if we'd used DUP instead of ?DUP, we'd have to DROP the extra 0 value generated on a loop exit somewhere after the REPEAT.

Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.