[MWForum]When or if
Wendy Petti
mwforum@lists.mathcats.com
Tue, 12 Nov 2002 17:28:15 -0600
> Another way of implementing the effects of WHEN could be:
> LAUNCH [FOREVER [IF condition [instruction list]]]
>
> Jeff
Regarding the above premise, may I humbly offer a quibble and a friendly disagreement:
1) [the quibble]
Using "launch" with "forever" is redundant. These two commands function the same:
launch [forever [if colorunder = 0 [fd 10 bk 10] ]]
forever [if colorunder = 0 [fd 10 bk 10] ]
2) [the friendly disagreement]
Contrary to your assertion above, the sample commands I just gave do NOT work the same as a replacement for this "when" command:
when [colorunder = 0] [fd 10 bk 10]
In the commands in (1) above, the turtle vibrates back and forth; in (2) the turtle simply moves fd 10 bk 10 once.
If you place the "when" command inside a procedure named "test" and place a "test" button on your page (setting it to "once") and click it, the turtle will run the second instruction just once if the the first instruction list is true, but the button will stay clicked on, because the computer is still evaluating the "when" condition.
If you set the button to "many times," the when command will run until we get an error message: I can't start a new prrocess in "test" - and the turtle will stop vibrating (all processes will stop running), but the button will still be clicked on until pressed again.
Just as we should not use a "when" command with a button set to "many times," we should never use "when" inside of a "forever" instruction, since "when" is constantly being evaluated and does not stop running; the computer will launch more and more and more of the "when" condition until it produces an error message. It is actually quite interesting to watch this happen if you have MWPro and look at the "processes" tab while running a doomed instruction such as:
forever [when [colorunder = 0] [fd 10 bk 10] ]
3) Putting this all together:
a) Using "if" with a "forever" command (or a button set to "many times") makes sense when you want the "if" command to be implemented endlessly as long as the condition is true.
b) Using "when" makes sense when you want the condition to be constantly evaluated but implemented only once when the condition is finally met.
c) Using "when" with "forever" (or a button set to "many times") never makes sense.
Wendy Petti
OpenWorld Learning
http://mia.openworldlearning.org
Math Cats
http://www.mathcats.com