Menu

The doc says "DUP before IF WHILE UNTIL are optimized away" . When would a DUP before an IF be useful?

Amt
2021-09-03
2021-09-03
  • Amt

    Amt - 2021-09-03

    Hi,
    I'm somewhat new to Forth. The documentation says that a DUP before an IF would be optimized but I don't understand a situation where a DUP before an IF would be useful. Could you help me with some examples?

    The only situation where I'd use DUP before an IF would be to check for zero/non-zero, but this is already handled with the 0= condition.

     
  • Mikael Nordman

    Mikael Nordman - 2021-09-03

    You would use DUP if you need a copy of the test value for other use later in the code.
    The optimization makes the test non-destructive of the top of stack.

     
    👍
    1

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.