Decisions

Decisions, Decisions...

"Two roads diverged in a wood, and I—
I took the one less traveled by,
And that has made all the difference.” —Robert Frost

It may not seem like a free-willed thinking agent, but our previous program made a decisionsort of. It chose between two possible actions, either to increase Mouse’s speed or to not increase Mouse’s speed. It made this decision many, many times depending on whether Cat and Mouse were touching.

In the Unit 1 activity, Flow Patterns, you explored the basic components that can constructs any algorithm: sequence, selection, and iteration. This type of “decision,” made by using the conditional if block, is an example of the algorithmic component known as selection. Much like the word “if” works in English, the conditional block only executes the blocks inside of it if something—some condition—occurs.

Think about the following English phrases:

  • “If you clean your room, I’ll give you $10.”
  • “If the bond measure passes, the new stadium will be built.”
  • “If you stay on the yellow brick road, you’ll reach Emerald City.”

Contrast these with the instruction blocks we’ve created up to this point, which have been more like imperative statements:

  • “Give me $10.”
  • “Build the stadium.”
  • “Travel to Emerald City.”
"I went into a clothes store and a lady came up to me and said ‘if you need anything, I’m Jill.’ I’ve never met anyone with a conditional identity before.”— Demetri Martin

If Blocks

The most basic type of conditional block is the if block. Sometimes, computer scientists refer to these types of instructions as branching. The following diagrams provide a visual explanation:

Consider a basic computer program much like the ones we’ve written up until now. The computer executes one instruction, then moves on to the next—the one immediately following it:

Instructions Direction of Flow

The arrow indicates the control flow, or how the computer moves from instruction to instruction over time. In other words, it starts at the top (the beginning) and moves downward (chaining instruction after instruction) from there, until it reaches the end. Note: Notice that the if block is contained in the Control tab—these are blocks that explicitly deal with flow control.

The if block however represents a choice. If some condition occurs, then the control flow branches off down a different path of instructions. If the condition does not occur, then the flow occurs as normal:

Instructions Direction of Flow

In this example, if the sprite is touching [mud], then its speed slows down by 5. Regardless, it moves speed number of steps afterward. The condition of touching [mud] just has an effect on the speed of the movement.

Scratch allows for easy visual indication of which blocks are affected by an if block; any block enclosed in the yellow frame of blocks headed by the if is executed only if the condition is met. Otherwise, they are simply skipped over.

Binary Conditions

Consider the “condition” in an if block. What kinds of conditions can be used in a program? Much like other aspects of computer science, it boils down to a bit (a dichotomy, a yes/no question, on or off)—a condition has two possible outcomes, either it is met or it is not met. In the Representation module, we will spend a great deal more time examining the power inherent in dichotomies.

Scratch “shape-codes” these binary values (also called Boolean values) as hexagons. There are many valid hexagon-shaped blocks available to fill the hexagon-shaped hole in an if block. Examine the following:

  1. The first is a Sensing block much like the ones we used in our Game of Tag. Either the sprite is touching the reddish color indicated, or it is not—there is no in between.
  2. The second is an Operator block. The Operators tab contains arithmetic operators like +, −, ×, and ÷, but it also contains Boolean operators, like <, >, and, or, not—operators that essentially have two outcomes (yes/no, true/false, etc.).
  3. The third uses multiple operators to define a more complex condition. Note that there is nothing preventing the condition from being silly, just as long as the condition can be true or false. When is the condition true and when is it false in this example?