Why does while loop execute more often than forever?

Questions about GP commands and how to do things

Moderator: MSandro

Post Reply
MrSteve
Posts: 61
Joined: Jun 16th, '16, 01:22

Why does while loop execute more often than forever?

Post by MrSteve » Jul 3rd, '16, 03:23

See attached project
whileVsFor.gpp
(5.36 KiB) Downloaded 139 times
, one sprite spins using forever and the other spins using while. The while cause a much faster spinning even though the turn values are the same.

Okay I can guess this has to do with the event loop to process scripts and perhaps each script has a maximum number of "steps" before it pauses to go to the next script. In any case the user does not have any control over this (at least as far as I can find so far).

Here I actually like the way Etoys did this using a "Ticks" value which could be set for each script where "Ticks per second" value could be used to control how "fast" a script ran.

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: Why does while loop execute more often than forever?

Post by JohnM » Jul 7th, '16, 12:41

Great question!

The "forever" loop is like all the loops in Scratch -- it does only one iteration of the loop per display cycle. That's perfect for animation, and it's great for helping novices understand what the computer is doing. But it's not so good for doing general computation. Fore example, using a repeat loop in Scratch to add up the number in a list with 300 elements would take 10 seconds (assuming a frame rate of 30 fps). In Scratch, you can get around this limitation using "turbo" mode, but that's a global setting, so it effects all loops. Sometimes you want to combine computations that run at full speed with animations that should do only one step per frame.

Originally, GP's forever loop was called "animate" to emphasize it's special property, but kids coming to GP from Scratch kept asking for the "forever" loop, so I changed the name to "forever". But perhaps it should be changed back to "animate" -- what do you think?

You can make other GP loops pause on every cycle by adding a "wait" block with no arguments, a special case that GP interprets at "wait for the next frame".

There isn't a direct equivalent of the EToys ticks-per-frame mechanism, but you can approximate it by nesting a "repeat" loop inside a "forever" loop:
Screen Shot 2016-07-07 at 8.41.27 AM.png
Screen Shot 2016-07-07 at 8.41.27 AM.png (18.71 KiB) Viewed 6178 times
The number of repeat iteration is similar to the ticks-per-frame in Etoys.

One other fine point is that GP will preempt a loop that runs for a long time so that the system remains "live". This mechanism is based on time, not iteration count. I believe the current timeout is 50 milliseconds. The loop isn't stopped, it's just forced to yield so that the display can be updated and user events handled; after that, the loop continues running where it left off. One place you'll see this effect is in the pixel manipulation projects -- even though it might take many seconds to process an image, you'll see the progress it is making because GP periodically updates the display.

MrSteve
Posts: 61
Joined: Jun 16th, '16, 01:22

Re: Why does while loop execute more often than forever?

Post by MrSteve » Jul 7th, '16, 21:15

Thanks for the detailed response.

JohnM asked:
Originally, GP's forever loop was called "animate" to emphasize it's special property, but kids coming to GP from Scratch kept asking for the "forever" loop, so I changed the name to "forever". But perhaps it should be changed back to "animate" -- what do you think?
Hmmm. Forever vs Animate vs Animar vs アニメイト vs حي
What is in a name? So I recall a story from Feynmam and how knowing the names of birds didn’t matter, what mattered is looking at the bird and noticing what it does (see excerpt below.
“The next Monday, when the fathers were all back at work, we kids were playing in a field. One kid says to me, “See that bird? What kind of bird is that?” I said, “I haven’t the slightest idea what kind of a bird it is.” He says, “It’s a brown-throated thrush. Your father doesn’t teach you anything!” But it was the opposite. He had already taught me: “See that bird?” he says. “It’s a Spencer’s warbler.” (I knew he didn’t know the real name.) “Well, in Italian, it’s a Chutto Lapittida. In Portuguese, it’s a Bom da Peida. In Chinese, it’s a Chung-long-tah, and in Japanese, it’s a Katano Tekeda. You can know the name of that bird in all the languages of the world, but when you’re finished, you’ll know absolutely nothing whatever about the bird. You’ll only know about humans in different places, and what they call the bird. So let’s look at the bird and see what it’s doing—that’s what counts.” (I learned very early the difference between knowing the name of something and knowing something.)

My father taught me to notice things. One day, I was playing with an “express wagon,” a little wagon with a railing around it. It had a ball in it, and when I pulled the wagon, I noticed something about the way the ball moved. I went to my father and said, “Say, Pop, I noticed something. When I pull the wagon, the ball rolls to the back of the wagon. And when I’m pulling it along and I suddenly stop, the ball rolls to the front of the wagon. Why is that?” “That, nobody knows,” he said. “The general principle is that things which are moving tend to keep on moving, and things which are standing still tend to stand still, unless you push them hard. This tendency is called ‘inertia,’ but nobody knows why it’s true.” Now, that’s a deep understanding. He didn’t just give me the name. He went on to say, “If you look from the side, you’ll see that it’s the back of the wagon that you’re pulling against the ball, and the ball stands still. As a matter of fact, from the friction it starts to move forward a little bit in relation to the ground. It doesn’t move back.” I ran back to the little wagon and set the ball up again and pulled the wagon. Looking sideways, I saw that indeed he was right. Relative to the sidewalk, it moved forward a little bit. That’s the way I was educated by my father, with those kinds of examples and discussions: no pressure—just lovely, interesting discussions.”
So in one sense it doesn’t matter what you call it, what matters is teaching students to notice things, experiment and figure out what they do and how they can use it.

Towards that end I think the built in help, examples, challenges and MOHQs for the students to gain understanding, explore and muck around are more important. I'll try and work on some projects toward that end to think about this some more.

Now in spite of my “what’s in a name” comment. My preference is towards calling it something different to help those who first learn with Scratch. Here is why - I often use the Feynman story (or some equivalent) with my students to get them to think about what something does and to encourage them to get a deeper understanding of things. When I teach beginning classes I usually use a combination of languages (usually Etoys/Scratch/Python). While I can hear the arguments that it will confuse the kids, getting them to see how to implement the same thing in different languages helps. I think the argument is similar to kids learning multiple languages when they grow up.

So my response to the kids who keep asking for the “forever” loop would be. Good Idea, what did the forever loop do? How did you use it? Then ask them to try the Animate loop to do something they used to use the forever loop for and see if that works for them.

doloop
Posts: 3
Joined: May 24th, '17, 02:32

Re: Why does while loop execute more often than forever?

Post by doloop » May 24th, '17, 18:02

Originally, GP's forever loop was called "animate" to emphasize it's special property, but kids coming to GP from Scratch kept asking for the "forever" loop, so I changed the name to "forever". But perhaps it should be changed back to "animate" -- what do you think?
FWIW, I vote for "animate." That name is suggestive of the importance of the display to its function.

Post Reply