Science / Grades 3-5
Programming basics: follow a changing value
Trace assignments and keep the current value of a named variable separate from displayed output.
The video could not load. The transcript is still available below.
Next: your worksheet
Programming Basics: Track a Variable
Checking sign-in...
Read the transcript
A name for a changing value
A program often needs to remember a value, such as a score. A variable gives that value a name we can use. Today we will track a variable called score. The name stays the same while its value can change.
Read our teaching notation
Our examples use pseudocode: readable instructions for explaining an idea, not one particular programming language. The first instruction says set score to two. Record two in the trace. We have given the name score a starting value we can use next.
Read before replacing
The next instruction says set score to score plus three. First read the old value, two. Add three to get five. Then replace the old value with five. Assignment is an update, not a claim that two and five are the same number.
Display is not an update
Now the instruction says show score. It displays the current value, five. In our pseudocode, showing the value does not add a point, erase the score, or change it in any other way. The scoreboard does not charge a point for looking.
Pause for a replacement
Suppose we add one more instruction: set score to one. Pause and predict the new value. Is it one, five, or six? Look carefully at the instruction. It does not say score plus one. It simply supplies a replacement value.
A new value takes the place
The new value is one. The previous five has been replaced. To increase five by one, the instruction would need to say set score to score plus one. A small difference in the written instruction can make a big difference in the result.
Digits can also be text
Fun fact: a digit can be stored as text rather than as a number. A program may need to handle those types differently. The label on a jersey looks numeric, but you do not add jersey labels to find a player's name. Always check what a value represents.
Keep a short trace
A trace records the value after each instruction, so you do not have to keep every change in your head. Continue to the worksheet and follow score through its updates. Explain which instructions replace a value and which one only displays it.