[MWForum]When or if

Wendy Petti mwforum@lists.mathcats.com
Tue, 12 Nov 2002 12:22:08 -0500


I will add to Jeff's and Daniel's explanations with an example of a
procedure in which I twice needed to use "when" instead of "if."

I created a typing race game which would display words to be typed,
along with a textbox in which the user would type.  I wanted a timer to
start the moment a user began to type in the empty user textbox, and I
wanted the timer to stop after 2 minutes and then announce the user's
number of words per minute.

I wrote a procedure to turn the timer on and off, and this procedure
would begin as soon as the student clicked a button to display a set of
words.  At first, I tried "if" commands to activate and deactivate the
timer, but since the computer only checked once near the beginning of
the procedure to see if the user textbox was empty (and it always *was*
empty at that point), the timer never became activated.  So then I
turned to "when" instead, like this:

to start.typing
resett
usertextbox, ct
when [not empty? usertextbox] [resett]
when [timer > 1200]
	[announce [Time's up!]
	score
	comment
	stopall]
end

Both of the "when" commands run constantly until the "stopall" command
is reached.

(resett = reset timer
"score" was another simple procedure to calculate the words per minute.
"comment" was another procedure to display a randomly-chosen
congratulatory remark or friendly insult depending on the number of
words typed.)

The one thing I don't understand about these primitives is the use of
brackets. "when" uses two sets of bracketed commands while "if" only
uses one set:

when [timer > 1200] [do-the-next-step]
if timer > 1200 [do-the-next-step]

I find this difference a bit puzzling and confusing.

Wendy Petti

> -----Original Message-----
> From: mwforum-admin@lists.mathcats.com
> [mailto:mwforum-admin@lists.mathcats.com]On Behalf Of Daniel Ajoy
> Sent: Tuesday, November 12, 2002 8:36 AM
> To: mwforum@lists.mathcats.com
> Subject: Re: [MWForum]When or if
>
>
> On 12 Nov 2002 at 21:42, Trish Sims wrote:
>
> > HelloAll
> >
> > I've been teaching year 8's (age 14) to use MicroWorlds.
> Lots of very
> > successful projects but I'm having difficulty understanding
> and explaining
> > the difference between proceedures that include
> >
> > when [touching? "t1 "t2] etc
> >
> > and
> >
> > if [touching? "t1 "t2] etc
> >
> > These procedures are used to especially in races to program
> colour or to
> > determine when/if turtles touch. Any suggestions would be greatly
> > appreciated.
> >
> > Trish Sims
> > Strathmore Secondary College
>
>
> "*When* checks continually to see if the condition in the
> first set of brackets is true. "
> http://mia.openworldlearning.org/ant_hill/ant_hill_5.htm
>
> ... and it does so independently of the normal flow of the
> program. This
> independent/continual check starts when the flow of the
> program reaches
> the *when* primitive.
>
> *If* only checks the condition when the flow of the program
> reaches the
> *if* primitive. (image attached)
>
> Daniel
> OpenWorld Learning
>
>