Forever-IF vs. IF-Forever: Programming in Scratch 2.0

Forever-IF vs. IF-Forever: Programming in Scratch 2.0


COLLEEN LEWIS: A common pattern is
using a Forever block with an IF block inside. To better understand this, we’ll look
at having an IF block with a Forever inside. Here in Scratch I’ve
written two scripts. The first one on the left
should look familiar. Here I play a note. Then I have a forever If. Forever I’m going to check if the
key space– that’s the Space key– is pressed. And if it is, then I’m going to play
the drum 1 for a quarter of a beat. So if I’m running this script,
I hear the note 60 play. And then whenever I press the
Space key, I should hear the drum. Let’s stop that one
and look at the next. On this one, again, it
starts by playing a note 60. And then one time, it’s going to check
if the key Space– the Space key– is pressed. Then forever, it’ll play this
drum 1 for a quarter beat. So it only checks if the
Space key is pressed once, and then forever it will play this drum. So if I start this script, it’ll
run quickly and stop running. So it plays the note, and then checks if
the Space key is pressed, and it’s not. So I never hear the drumbeat. If I press the Spacebar, and then I
start this script, it’ll play the note, the key will be pressed, and forever
it’ll keep playing this drum. So let me try it. I’m holding down the Space key, and
now I’ll double click this script. Even if I let go of the Space key,
it’s not checking that anymore. It’s just in this forever,
playing this drum. Let me stop it. So in the one on the left, where
forever was on the outside, it forever repeated this test
of if the Space key was pressed. The one on the right only checked
if the Space key was pressed once. But if it was, then forever
it played this drum. Forever and If can be
pretty tricky, so I wanted to show you two
different examples where we use them in a different order. Really you’re only ever going to
want to use the one on the left. That one’s going to be pretty helpful.

1 thought on “Forever-IF vs. IF-Forever: Programming in Scratch 2.0”

Leave a Reply

Your email address will not be published. Required fields are marked *