Tracing 1A

To better understand programs, and often to debug them, programmers use pen and pencil tracing.

To trace, step through a program line-by-line, showing the value of each variable.

When a new variable is introduced, create a memory cell for it and place its value in it:   

        x = 5          x
                          5


When variables change, cross out the old value and put the new
value underneath.


        x = 5          x
        x=x+1        5

                          6

In this way, the trace shows the history of the program execution, how each variable changes over time. Note, however, that each (scalar) variable represents a single cell with one value. In terms of program execution, the crossed out values are gone forever.

Instructor Demo: Interest Rate Program

principal = 5000
interestRate = .1
oneYearInterest = principal * interestRate
oneYearPrincipal = principal+oneYearInterest
twoYearPrincipal = oneYearPrincipal+(oneYearPrincipal*interestRate)



In-Class Worksheet: Programmer Milestone 2-- total of a list

list = [4,2,19,3]
i=0
total=0
while i<len(list):
    total = total+list[i]
    i=i+1
print total

Recent site activity