Building Blocks of Programming in Scratch
in Summer Institute on Scratch
Computer programming is about building and designing executable computer solutions (programs) to specific computing tasks. The job of a computer programmer usually involves coding, debugging, documentation, and testing (all of which are emphasized in this course). However, to successful complete these tasks, an aspiring programmer must first understand the fundamental building blocks.
Statements
In programming, a statement is a command that tells the computer to do something. In Scratch, any block that reads like a command is a statement. For example, the following block instructs a sprite to say something. Another block commands the sprite to go to the mouse’s x and y location:
You may want a statement to be executed only under specific conditions. Such conditions are defined in terms of boolean expressions.
Boolean
A boolean expression is an expression that is either true or false. In Scratch, any diamond-shaped block is a boolean expression. For example, the following block evaluates to true if the mouse button is down or false if not. Another example is the comparator block which will evaluate to true is some number is less than another and false if not.
With boolean expressions we can make conditions.
Conditions
A condition must be true in order for something to happen. In Scratch, any block whose label include the words “if,” “when,” or “until” is a conditional construct. For example:
This if construct can instruct a sprite to say hello if the user has clicked the mouse:
The if-else construct is different in that we can instruct a sprite to, if the mouse is clicked, say hello. Else (the mouse is not clicked), say goodbye.
Furthermore, these constructs can be nested to allow for multiple different conditions:
Other conditional blocks include:
Sometimes, you may want statements to be executed many times in a row. Loops make this behavior possible.
Loops
A loop can make one or more statements execute multiple times. In Scratch, any block whose label begins with “forever” or “repeat” is a looping construct.
An instance of a loop construct is the forever block which allows us to, for example, instruct a sprite to meow every other second:
The repeat block below allows you to loop a specified number of times.
The repeat until block allows you to loop until some boolean expression is true.
Variables
In programming, variables allow a program to “remember” a value. In algebra, x and y are popular variables for storing the numerical value of coordinates such as (3, 4). In Scratch, variables are the oval blocks that must be uniquely labelled (no two variables can have the same name).
Variables can be local or global. In Scratch, a local variable is used by only one sprite. A global variable can be used by all of your sprites.
Variables allow, for example, a sprite to count up from 1:
Statements, boolean expressions, conditions, loops, and variables are the building blocks in programming. Now, we explore higher-level programming constructs such as threads and events.
Threads
In general, a thread is mini-program running within a larger program. A program can have multiple threads and, therefore, can do multiple things at the same time. In Scratch any block with a wavy top and whose label begins with “when” demarks the start of a thread. For example, the “when flag clicked” block and the blocks that fit unto it form a thread that executes when the user clicks the green flag.
It is helpful to separate different tasks into different threads. For example, you might want to keep track of whether the user presses a key to turn off and on (toggle) the sound.
In the above example, the left-hand thread is responsible for meowing and the right-hand thread is constantly checking and remembering whether the user has muted or unmuted sound by pressing the space bar.
Events
Multiple threads can communicate with each other by signaling and then handling events. An event is like a message between threads. In Scratch, broadcast blocks signal events to blocks that begin with “when” (threads).
Clicking the Scratch green flag, for example, signals an event that is handled by
In Scratch, events also allow different sprites to communicate with each other. For instance, two sprites playing Marco Polo require a message to be sent. Therefore, an event is broadcasted.