Chapter 2

CT Concepts

The Scratch Computational Thinking Concepts Explained

Subsections of CT Concepts

Sequence

At the core, a program is simply a step-by-step set of instructions the computer will carry out. In Scratch, these instructions take the form of blocks, which snap together in a sequence, which we typically refer to as a block stack (or stack).

For a sequence to be executed (carried out), it must start with a “hat” block, so called because its shape resembles a baseball cap:

Hat block shape Hat block shape

This block indicates the conditions that will trigger the execution of blocks that are snapped into place beneath it. The commands of these other blocks will be carried out in order - a sequence. For example, the following script will play the first two measures of the tune “Hot Cross Buns”:

Hot Cross Buns script Hot Cross Buns script

Clicking the green flag will play each note in the order they appear in the sequence. You can see the full program here or run it below:

If we leave off the hat block, then the sequence of blocks will never be executed. We call a stack without a hat block an orphan stack.

Often, programmers will leave a sequence of blocks that they may come back to work on later orphaned intentionally, as they might comment out sections of code in another programming langauge. However, best practice is to remove orphaned code once you are sure it is no longer needed, as having many orphan stacks makes the script harder to read and understand.

You can have multiple stacks in a Scratch program, and each will execute when its starting condition is met. For example, if two stacks both start with a when flag clicked block, both will run when the green flag is clicked. This is covered in more detail in the paralleism section.

Data

Strings

Numbers

Booleans

Variables

Lists

Operators

String Operators

Mathematical Operators

Boolean Operators

Conditionals

Loops

Events

In programming, an event triggers the execution of code. You’ve already worked extensively with one event in Scratch, the when flag clicked block:

when flag clicked when flag clicked

Clicking the green flag button on the stage triggers the hat block to execute.

A related event is triggered when you click the red stop sign button. That button stops all running scripts, effectively stopping the program. You can also trigger this event with the stop () block:

stop () stop ()

In addition to stopping all scripts in the program, the stop () block can be used to stop the other scripts of this sprite, or the specific script it appears in.

There are many other events and blocks that interact with them in Scratch. Let’s examine each.

User Events

User events are events triggered in some way by the user. The green flag is the most common example - clicking it will execute any stack of blocks starting with a when flag clicked block:

when flag clicked block when flag clicked block

Likewise, the when () key pressed block will execute its block stack when the specified key is pressed. You can set it to the arrow keys, letter keys, space, or ‘any’ key:

when () key pressed block when () key pressed block

You can also use the when this sprite clicked block to trigger a stack of blocks when the user clicks on its sprite.

when this sprite clicked when this sprite clicked

Finally, a unique event in Scratch is represented by the when [loudness] > () which will trigger when a microphone attached to the computer detects sound louder than the specified threshold:

when [loudness] > () block when [loudness] > () block

Timer Events

Scratch includes a timer that starts when the green flag is clicked, and counts up in seconds. Its value can be accessed with the timer reporter block, and it can be restarted with the reset timer block. Both are found in the Sensing palette.

timer timer

reset-timer reset-timer

In addition to these two blocks, the when [timer v] > () block can be used to trigger a block stack when the timer reaches a certain value, i.e. 10 seconds:

When timer > 10 When timer > 10

Backdrop Events

The when backdrop switches to () block triggers when the backdrop is changed to the selected value:

when backdrop switches to () when backdrop switches to ()

This can be especially helpful when a program is organized as a state machine with each backdrop representing a different state. One example of this approach would be an animated storybook with each backdrop serving as a new “page” of the story.

Clone Events

When a new clone is created it executes any block stacks starting with the when I start as a clone hat block:

when I start as a clone when I start as a clone

Broadcast Events

Finally (and perhaps most powerfully), the programmer can create custom events in Scratch using the broadcast () or broadcast () and wait blocks. These blocks send a message that executes any corresponding when I receive () hat blocks:

when I receive () when I receive ()

A new message can be created by selecting the “New message” option in any of these block’s selection menus:

new message option new message option

The broadcast () block sends the corresponding message and immediately continues executing blocks in its own stack:

broadcast () broadcast ()

In contrast, the broadcast () and wait sends the message and then pauses its stack’s execution until all when I recieve () stacks have finished executing:

broadcast () and wait broadcast () and wait

Note this does not necessarily mean the entire stacks are executed before the pause ends, as the other stacks will pause at wait () seconds and at each loop iteration. See the parallelism section for more details.

Parallelism

Parallelism refers to doing more than one thing in parallel (at the same time). When we have two stacks of blocks both starting with a when flag is clicked, these both trigger thier execution when the green flag button is clicked, and appear to execute at the same time. Likewise, two stacks starting with when I recieve () blocks listening for the same message will be triggered at the same time.

Parallism exists between stacks of blocks in the same Sprite, and also between all Sprites (and the Stage) - essentially any time an event occurs, each corresponding hat block will execute.

Ordering and Synchronization

However, there is some order to parallel actions in Scratch. Consider these scripts:

parallel say and think block stacks parallel say and think block stacks

The sprite containing them can only display one of the speech/thought baloons at a time, so one of these will take precidence over the other. Generally, they will execute in the order the stacks were created, but there is no guarantee of this.

Establishing an order to script execution is called synchronization, and there are several techiques we can employ.

Yielding in a Stack

One of the easiest is to add a wait (0) seconds block in the stack we want to go last. A wait block pauses the execution of the current stack, and allows other stacks to finish their execution. Setting the seconds to 0 means that the pause is infantesmally small, but it does allow the other block stacks to run first (we call this a yield in computer science). In Scratch, loops also yield after each iteration.

While this approach works, it is not exactly intuitive, and it only ensures ordering between two stacks. However, it is important to know, as it can help understand the behavior of parallel stacks.

Message-based Synchronization

A far more powerful approach is using broadcast () and when I recieve () blocks to trigger stacks in sequence. For example, the following script first executes the say () for () seconds block, then triggers the think () for () seconds block:

While the example is trivial, this appraoch to synchronizing parallel stacks is extremely powerful. You can have multiple stacks in multiple sprites triggered by each message, and you can define as many messages as you need.

One common design pattern using this approach is the Get Ready and Go Design Pattern, which helps ensure all sprites start in the expected state when a Scratch program is run.