Skip to main content

0.5 Programming Constructs

You may have been through any one of the Graphical Programming Studios above, We’ll summarize it as a general case with the Programming Constructs in Scratch, developed by MIT’s Media Lab.

So functions like say in Scratch will be a purple puzzle piece:


These are like actions, or verbs, that do one thing.





We also have conditions, where we have a branch that may or may not happen depending on the Boolean expression, in this case x < y, inside:



Notice that the action inside is wrapped inside the if block.

We can nest more conditions inside:



We have a threeway fork now, where one of them will be true.
We can compare variables to numbers in our Boolean expressions:



We can also have blocks that repeat forever:



or for a finite number of times:


Both of these are examples of loops.
We can set variables to a certain value:



And finally, we can have more than one of these:



Scratch, as well as other languages, support multithreading, or the ability for a computer program to do multiple things at once. Here, if we created two sets of scripts with a "when green flag clicked" block at the top of each, both will start running at the same time when we indeed click the green flag.
We’ll also see the concept of event handling, which essentially allows different piece of our program to communicate to one another:



Now let’s start with some simple programs! We can drag the following blocks together, to have our cat make a "meow" sound three times:




First, we tried the play sound [meow] block by itself, but we only heard "meow" once. That’s because we played the sound, and immediately repeated that three times, so all three plays happened very quickly one after another. By using the play sound [meow] until done block, we can hear all three plays.
And adding wait (1) secs makes our cat sound a little more natural.
We can drag the following blocks together, experimenting as we go along:



Now our cat will move back and forth on the stage forever!
By putting together these small pieces, we can build more and more complicated programs.
We demonstrate several Scratch projects, noting that for each one, perhaps a small part was implemented at a time.
Scratch also supports setting variables to random integers, which helps us build games with more variety.
We can look at how variables are used in this simple program, where a sheep on the stage starts counting from 0:



Here, counter is what we named a variable, with which we are storing the current number that the sheep is on.
And we can use interactive Boolean expressions that automatically capture our input:



Now if we click the green flag and move our mouse pointer over the cat on the stage, it meows!
We can also have interaction between two sprites, or characters, on the stage. Here we have a cat:



It starts by choosing a random direction, then turns toward the bird and moves toward it 1 step at a time.
The bird also has a similar script where it moves some number of steps at a time, bouncing from the edge as needed.
Events, too, can be implemented with a few blocks:



We see the broadcast [event] block for one of the sprites, and the "event" can be heard by other sprites like so:



A single sprite, too, can have multiple threads that can share variables:



Here the muted variable is checked by the piece of the program that makes a sound, but it can be changed by the piece on the left that checks for whether the space key is pressed.


In the next section, We shall move to a more traditional Programming Language and see the similarities between the two languages.

Comments