16 Assigning values to variables and solving problems with Scratch

Sometimes we need variables.

We already saw when we looked at if-then rules that sometimes we want the algorithm to do things depending on inputs or on the values of some variables. In many cases we need to introduce new variables, or keep on updating existing variables in order to solve problems. For example, if we want to check if a number is prime, we will have a loop that checks the divisibility of a number at each stage. But, then we need to update the number we’re checking. Whether we are introducing variables, or updating existing ones, this process is referred to as ‘variable assignment’.

Different programming languages will have different notation for assignment. For the algorithm that counted people in a room, we had an initial assignment [latex]N \leftarrow[/latex] which was read as ‘let N equal 0.’  Then, after counting each person there was an updated assignment:  [latex]N \leftarrow N+1[/latex]

Or, set N to N + 1.  This kind of grammar differs to how we often think or work in mathematics, because we are not usually working in a dynamic environment, and so [latex]n=n+1[/latex] doesn’t make sense.

Let’s have a look at some simple loops that involve assigning and updating variables.

Assigning variables in Scratch

Transcript

The counting cat

Transcript

Guessing game

Transcript

Keeping track

When you start making games or algorithms, one of the most difficult things is keeping track of all the variables and all the possibilities. You have to account for each path the algorithm might take, otherwise if a case isn’t accounted for, the code might crash or just not run as you expect.

Coding

  • Try to design your own simple game. Start with the idea, then map it out as a process and make sure you know how to implement each step (and that it won’t take too long) and then have a go at playing it yourself (or show someone else). If you are interested in making small interactive games in Scratch, you can also incorporate additional sprites (like the cat). You can set these sprites up to move themselves and use events keys like “when spacebar clicked” to control your character.
  • Try to design your own simple algorithm that solves a problem. This could be a simple calculation that is based on multiple user inputs, or it might be something more difficult.

Debugging

Can you fix this Scratch program so that it works properly?  You’ll need to click the ‘see inside’ button.

Other block-code programming languages

There’s more out there for you (or your students) than just Scratch.  There are a number of other simple programming languages that work similarly (or the same as) scratch.  In particular, you can program Jimu robots and Sphero robots using similar commands.  There’s a version of Scratch on the iPad called “Scratch Junior” that has a simplified set of commands and there’s also Logo, which is a programming language that can be used for drawing shapes.

You may wish to explore any of these.

License

Icon for the Creative Commons Attribution-NonCommercial 4.0 International License

Mathematical Reasoning and Investigation Copyright © 2023 by Deakin University is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License, except where otherwise noted.

Share This Book