## Variables – Execution Flow

The value associated with a variable typically changes as the program executes. Variables represent a piece of memory that supports two different operations: reading from the variable; writing to the variable. Reading occurs when the memory location where a value is stored is accessed. Writing occurs when the program changes the value stored in a variable (through an assignment statement).  For example,

a = 10

b = 15

a = a + b

After the three statements have been executed, variable b still has the value of 15 and variable a has the value of 25.

Understanding the flow of execution is important and some students find it a difficult concept.  Let students write short programs to see how the values of variables change over time.   Make sure students have mastered it before moving on to writing code with conditionals or  loops.

Example: Execution Flow Activity

The goal of this activity is to help students understand how variables work. It is similar to  our previous example with the marbles and cups.

For this activity, you will need some set of small objects (beads or marbles, for example), and a desk organizer (one of those cabinets with little drawers you can put paperclips and thumbtacks in). For the purpose of the demonstration, you will label each drawer with a letter (x, y, z, etc).

Explain to the students that the drawers represent variables. This is a reasonable comparison since a computer variable is nothing more than “a place to store something”, such as a numerical value. The value stored in each variable will be represented by the small objects.

In the beginning you can explain that each variable (drawer) is empty because you haven’t stored anything in it. For many programming languages, this mimics the true behavior in which a new variable is initialized to zero.

Now show your students a simple snippet of code like the following:

x = 7

y = 2

z = x - y

y = x - 6

y = y + 1

Talk through this piece of code as described below. Note that actions are marked in bracketed and underlined italics.

• The first instruction says to store 7 marbles in drawer x. [put 7 marbles in drawer x]
• The second instruction says to store 2 marbles in drawer y. [put 2 marbles in drawer y]
• The third instruction says to find out how many marbles are in drawer x, [open drawer x again, count the marbles and write this value down] and subtract the number of marbles in drawer y [open drawer y and count the marbles, write it down] and put that many marbles in drawer z. [7 – 2 = 5. put 5 marbles in drawer z]
• The fourth instruction says to find out how many marbles are in drawer x [once again, count how many marbles are in drawer x and write it down], subtract six from that [write out the calculation 7 – 6 = 1] and put that many marbles in drawer x. [count the marbles in drawer y] Oh, wait a minute, this drawer already has marbles in it. We need to take them out, and put in one. [take out all marbles, put one in]
• The last instruction says to find out how many marbles are in drawer y [count the marbles in drawer y] and add one. And that’s how many marbles we’re supposed to have in drawer y. [empty drawer y, then put 2 marbles in]

Once you have walked through this process, manually putting marbles in and taking them out, have the students try. Write code snippets down and have students act them out. After going through several examples, give exercises for students to try just using pencil and paper.