Building Blocks of Programming in Scratch

General overview of programmatic constructs such as statements, boolean expressions, conditions, loops, variables, threads, and events.

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. say hello Another block commands the sprite to go to the mouse’s x and y location: goto

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. mouse-down Another example is the comparator block less-than 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: if-construct

This if construct can instruct a sprite to say hello if the user has clicked the mouse: if-example

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. if-else-construct

Furthermore, these constructs can be nested to allow for multiple different conditions:

nested-condition

Other conditional blocks include: keypressed wait-until

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: forever-example

The repeat block below allows you to loop a specified number of times. repeat

The repeat until block allows you to loop until some boolean expression is true. repeat-until

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: count-variable-example

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. flagclick

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.

multithreading

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). broadcast

Clicking the Scratch green flag, for example, signals an event that is handled by flagclick

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. marcopolo

Debugging exercises

Review

Scratch block images from David Malan

© 2020. Some rights reserved.