Ever heard the term control flow? It wouldn’t surprise me if you hadn’t: it’s not a concept that’s always explicitly addressed when you start learning how to program. It’s a bit of a doozy to understand, but with any luck, you’ll have a clearer picture of what I mean by the end of this article!

 

What is control flow?

The term control flow refers to the order in which the instructions in your program are executed. Still a little bit lost? Consider this Scratch program:

 

A Scratch program.

 

What the above program does is: when the space key is pressed, all of the blocks beneath it are executed. Now, each block in this program tells the computer to do something, right? So let’s think of each individual block as an instruction. When you hit the space key, in what order are the remaining instructions executed? It is:

  • A: alphabetical?
  • B: based on colour?
  • C: completely random?

 

The answer is D: none of the above. If you run the program, you’ll see that the instructions are executed in a top-down structure: in other words, it starts at the top and works its way down through the instructions until it reaches the bottom. You could say that its control flow is sequential or linear, because it runs through the instructions in the aforementioned way: you’ll see that the program —

  • Makes Scratch Cat say “Hello” for 2 seconds
  • Rotates Scratch Cat clockwise by 180 degrees
  • Waits for 1 second
  • Makes Scratch Cat think “Hmm…” for 2 seconds
  • Resets Scratch Cat’s orientation, so that he’s facing the right

 

You could say that the control flow of the program we dissected earlier looks a little something like this:

The control flow of the aforementioned Scratch project.

 

But a program’s control flow is not always so straightforward. In fact, it’s very common to have instructions in your code that are repeated, or branching paths in the control flow of your program. There’s an entire category of statements that alter the control flow of your program: they’re called control flow statements.

 

Control flow statements

The types of control flow statements available to you do depend on the language that you’re programming in, but today we’ll only go over the types of control flow statements that you’ll find in Scratch: loops, and decision-making statements. You can find these statements in the Control category.

 

Loops

Loops are a really fundamental concept in programming. A loop is essentially something that will continue to repeat until a certain condition is met (i.e. until a certain condition becomes true). There are three loop blocks that you’ll find in Scratch:

 

The three loops you will find in Scratch.

 

The Repeat X loop

The Repeat X loop repeats the instructions contained within it for a specified number of times. So, if you had a program like this:

 

An example of how to use the Repeat X block.

 

Then Scratch Cat would turn 15 degrees clockwise for a total of ten times. The instructions within the repeat block are looped until the condition — “repeat this instruction a total of ten times” — is met, i.e. it is true that the instructions contained within the loop have been repeated ten times.

Its control flow looks a little something like this:

 

 

The control flow of the aforementioned loop.

 

The Forever loop

The Forever loop repeats the instructions contained within it forever. So if you had a program like this:

 

An example of how to use the forever loop.

 

Then Scratch Cat would turn 15 degrees clockwise forever!

 

WARNING: Do beware of infinite loops! They don’t matter so much in Scratch, because you can always manually stop a program by clicking the red stop button. That being said, it’s always good practice to put fail safes into your program! If you’re playing with forever loops, be sure to make good use of the Stop X block, which pairs rather nicely with the forever loop:

 

The infamous stop block — very useful when paired with the forever loop!

 

 

The Repeat Until X loop

The Repeat Until X loop repeats the instructions contained within it until the specified condition is met. You can create the condition using a Boolean (the blocks with triangular ends that you can find in the Sensing or Operator categories). Here are some of the Booleans that you can find in Scratch:

 

Just some of the booleans that you can find in Scratch.

 

You don’t need to know what Booleans are yet, but just know that a Boolean either contains the value true or false. When a Boolean is true, Scratch will interpret this as “the condition has been met”.

 

Decision-making statements

There are two decision-making statements that you can find in Scratch: if-then, and if-then-else.

 

The two decision-making statements in Scratch: if-then, and if-then-else.

 

The if-then block

The if-then block executes the instructions contained within it if the specified condition is met. So, consider a program like this:

 

 

A program using the if-then block.

 

What this program does is it generates two random numbers, both between 1 and 10. If these two numbers are equal (e.g. if the number 5 is produced in both instances), then the condition for the execution of the above if-then block will have been met.

However, if the two numbers are not equal, then the statement will be false, and the if-then block will never be executed.

 

The if-then-else block

The if-then block executes the instructions contained in the top section if the specified condition is met. If the specified condition is not met, then the instructions contained in the bottom section will be executed instead.

Here’s an example of the if-then-else block in an action, building on the example from above:

 

A program using the if-then-else block.

 

Just like in the if-then block example, the program checks to see if two randomly generated numbers are equal. If they are, then Scratch Cat says, “We have a matching pair!”

In the if-then block example, if the pair of numbers is not equal, nothing happens. But in this example, if the pair of numbers is not equal, then the instructions in the “else” section are executed, and Scratch Cat will say, “No matching pair just yet…”

The control flow of the above program looks a little something like this:

 

 

So, let’s summarise everything we’ve learned so far:

Control flow is the term used to refer to the order in which the instructions in a program are executed. The control flow of a simple program might be linear or sequential, but through the use of control flow statements, you can alter your program’s control flow.

Some examples of control flow statements in Scratch are loops — which cause instructions to repeat until a certain condition is met — and decision-making statements, which can create branching paths in your code.

Happy Scratching!

 

If you liked this article, why not get our free e-book for teachers to learn more about the fundamentals of computer programming and how to integrate these concepts into the classroom?

 

Check out our free teacher download. Learn to code with Scratch in 30 minutes.


If you’re interested to find out more