teacher note Teacher Note: Limits of the box analogy

In this book we use a labelled box as an analogy for variables. However, this analogy only goes so far. Below are a few of the misconceptions students form when thinking of variables as boxes, compared to how variables actually work:

Boxes can contain more than one value at a time

On the other hand, variables can only contain one value at a time. Take the following sequence of assignment statements:

If the variable name were a box, what does it now contain after these two assignments? This is the correct behavior: “Mary” will replace “Jane” as the value of name. You can verify this by running the above code and observing the output.

../_images/boxes-analogy-right.gif

Figure 1: Correct model for successive assignments.

However, if the student misapplied the box analogy, she might say that name now contains both “Jane” and “Mary”.

../_images/boxes-analogy-wrong.gif

Figure 2: Incorrect model for successive assignments.

You take an object out of a box before placing it in another

Students may take this prior understanding of boxes and misapply them to variables when dealing with assignment. For example, what is printed out after this running this program?

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

What happens during the assignment in line 2? The value of name1 is copied into name2. The correct prediction to make is that “Jane” is printed two times.

../_images/boxes-move-right.gif

Figure 3: Correct model for assigning a value from one variable to another.

However, a student may interpret line 2 to mean “take the value in the box name1 and move it to the box name2“. This will leave nothing in name1, leaving it empty. She may predict then that the first print will display nothing, while the second print will display “Jane”. This is incorrect.

../_images/boxes-move-wrong.gif

Figure 4: Incorrect model for assigning a value from one variable to another.

Boxes may be a useful analogy to understand variables, but it’s important to highlight that not everything about boxes applies.

You have attempted 1 of 3 activities on this page