Grok Learning
Published in

Grok Learning

Programming in Primary School: Introducing if statements

Previously, I’ve discussed introducing young coders to the concept of sequencing — defining a set of steps for a computer to follow in order. Our Monster Maker course teaches students to write their first programs as a specific sequence of steps that run the same way every time.

Programs like this are quite limited. Typically, we want our programs to be able to do different things depending on what we need. For example, we don’t want to see exactly the same list of websites every time we perform a Google search — we want the results to reflect the search term we entered!

Computers can be programmed to make decisions — to do different things in different circumstances — and this is one of the things that make them so powerful.

The Australian Digital Technologies curriculum specifies that students should start working with programs that can make decisions from grade 3 onward:

Implement simple digital solutions as visual programs with algorithms involving branching (decisions) and user input (ACTDIP011)

In this post, I’ll discuss how to use if statements to make decisions, and share some ideas for introducing these concepts in the classroom.

How do computers make decisions?

In many programming languages, decisions (also called conditionals) take the form of an if-then construct. They start with a condition, which is then evaluated as either True or False.

Below you can see the same program written in two different programming languages; Blockly and Python:

A decision written in Blockly
A decision written in Python

Both programs start by asking the question: “Are you going out to play?”.
The condition is the user’s response - the program checks if the response is “yes”.

If the response is yes, the condition is True and the program will print Put on a hat!
If the response is not yes, the condition is False and the program will not print anything.

If statements like this are called control structures, because they control the flow of a program. You can see this more clearly if we map out our program with a flowchart:

Decisions with only one outcome like this are a bit limiting. Sometimes we want our programs to also do something when a condition is False. We can do this by adding an else clause to the if statement:

A Blockly if statement with an else clause
A Python if statement with an else clause

Now our program can perform one action if the condition is True, and a different action if the condition is False.

Introducing if statements in the classroom

The formal logic of if statements can be a little tricky for young students to get their heads around, but most students will already have had plenty of experience with if-then decisions in their daily lives!

Take the classic Stanford marshmallow experiment. Young children clearly understand that if they leave the marshmallow on the plate then they will be rewarded with a second marshmallow (even if it takes all of their willpower!).

A video like this offers a good way to start discussing some of these concepts with students:

  • What is the reward? A second marshmallow
  • What is the condition of the reward? Leaving the first marshmallow on the plate
  • Will the child always get the reward, no matter what? No! They only get the reward if the condition is True. If the condition is False they will get no reward.

Students can probably come up with plenty of their own examples, such as:

  • If I finish dinner then I can have dessert.
  • If my homework is done then I can go outside to play.
  • If it is a Tuesday then I have ballet class.

Once students have a handle on if statements, they can add an else clause. For example:

  • If it’s raining then I wear my gumboots, else I wear my runners.
  • If it’s Saturday then I watch a movie after dinner, else I read a book.
  • If there is chocolate then I will pick that flavour ice-cream, else I will pick strawberry.

Emphasise that students should try to come up with a different action for the else clause (e.g. If it’s raining then I wear my gumboots, else I wear my runners), not just a lack of action (e.g. If it’s raining then I wear my gumboots, else I don’t wear my gumboots.)

More unplugged ideas:

  • Play an if/else game as a class. A leader provides if/else instructions that the rest of the class needs to follow, e.g. “If you are wearing a red shirt put your hands on your head, else put your hands on your knees”.
  • Get students to map their if and if-else statements with a flowchart, between a ‘start’ and an ‘end’. This is a good way to get students thinking about the way that conditionals determine the flow of a program, and control which actions do and do not take place.

Writing programs with branching and user input

Unplugged activities are a good start, but the DT curriculum requires students to ultimately implement digital solutions as visual programs.

Our More Monster Maker course has been specifically designed to help teachers implement this part of the curriculum. Students use Grok’s custom version of Blockly, a visual programming language with drag-and-drop blocks, to write programs which can ask for user input, make decisions, and draw more monsters!

Let’s take a look at an example from the course.

Our program starts by following the first four blocks in sequential order, to draw a monster:

The orange block contains an if statement. The first thing it does is ask for some user input:

In our simplified version of Blockly, questions can be answered with either a Yes or a No:

The second thing the orange block does is evaluate our if statement. In this case, the program is checking for the answer yes. If we do answer Yes, the if statement is evaluated as True and the block inside is executed to draw a hat on out monster:

If we answer No, the condition is False and the instruction to draw a hat is skipped:

As students progress through the course they write programs using more complex logic, including decisions with an else clause and nested if statements.

Along with our original Monster Maker course, More Monster Maker is currently available as part of our Primary Pack. If you are a teacher (or a teacher in training!) you can take a look at all of our resources with a free all-access subscription.

So, do you have any tips or tricks for teaching students about decisions? We’d love to hear them!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store