2. Introduction to Trace Tables
A tool for tracking variable values during program execution
Helps understand program flow and identify errors
Consists of variable names as column headings and values
in cells
One row for each pass or iteration of the program
3. Components of a Trace Table
Column headers: Variable names (identifiers)
Rows: Each pass or iteration of the program
Cells: Values of variables at each step
Additional columns can include line numbers or conditions
Why do you think tracking variables is important in
programming?
4. Creating a Trace Table: Step 1
Identify all variables in the program
Create column headers for each variable
Add a column for line numbers if needed
Example variables: count, sum, average
5. Creating a Trace Table: Step 2
Go through the program line by line
Update variable values in each row as they change
Note any conditions or decisions made
How might this process help in debugging a program?
7. Example: Simple Counting Loop
Program: Trace a program that counts from 1 to 5 and
prints the count.
START
Declare count as integer
count = 0
While count < 5:
print(count)
count = count + 1
ENDWHILE
STOP
(continues until count = 5)
9. Example: Calculating Average
Program:Trace a program that finds the sum and average of
numbers from 1 to 4
START
Declare sum,count as integer
sum = 0
for i =1 to 4 do
sum = sum + i
average = sum / 3
ENDFOR
STOP
13. Common Mistakes to Avoid
Forgetting to update all variables in each row
Misinterpreting logical operations in truth tables
Skipping steps in complex programs
Not considering all possible input combinations
What strategies can you use to avoid these mistakes?
14. Practice: Creating a Trace Table
Let's practice together!
Program:
x = 5
y = 0
while x > 0:
y = y + x
x = x - 1
Create a trace table for this program
What is the final value of y?