Skip to main content
AP CSP course

C18 · Topic 3.1 · 40 minutes · AAP-1.A, AAP-1.B

Tracing assignments

By the end you can:

  • Track state after every assignment.
  • Preserve a value before overwriting it.
  • Use a distinguishing test for a swap.

Before this lesson: topics 1.2.

Variable
A named reference used to represent a value that can change during a computation.
Assignment
Storing the value of an evaluated expression in the named variable.

Understand

A trace table records the value of each relevant variable after each statement. Evaluate one instruction at a time using the state produced by earlier instructions. The order matters: reading a variable after an overwrite uses the new value, not the value it had at the start.

To exchange two values with ordinary sequential assignment, preserve an original before replacing it. The sequence a←b;b←a loses the original a. Equal starting values can conceal this defect, so choose a test where the originals differ and specify what a successful exchange means.

Worked example

Compare destructive overwrite and a temporary-variable exchange starting a 4,b 9.

temp ← a
a ← b
b ← temp
  1. temp stores the original 4.
  2. a becomes 9 while b remains 9.
  3. b then receives 4 from temp, yielding[9,4], unlike the faulty[9,9].

Explore each concept

Choose a concept, change its inputs and follow the explanation. Each example models the stated rule or scenario; completing it is practice rather than evidence of mastery.

  • Names versus values
  • Number, string, Boolean and list
  • Copy then update
Open this example on its own

Change it · step through · explain

Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.

Ready · Make a prediction

Ready to explore: Value types and independent copies
  1. Value typenumberWaiting
  2. Original number (0–99)7Waiting

Ready to explore: Value types and independent copies

Why: Change an input, predict the first effect, then advance one step.

Try this: Change one input or choose another listed case. Predict the effect, reset and compare the states and governing rule.

Step through and experiment

Change it · step through · explain

Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.

Ready · Make a prediction

Ready to experiment
  1. Input 14Waiting
  2. Input 29Waiting
  3. Input 35Waiting

Ready to experiment

Why: Predict the first change, then advance one step.

Try this: Change one input, predict the difference, then reset and compare.

Check a misconception

Claim: The right side of every statement uses the original starting values.

Sequential execution uses the values in state at that particular step.

Guided practice

Trace a←4;b←9;a←b;b←a and compare with the temporary exchange.

Work with a partner. Make a prediction, compare your reasoning, then identify the rule that supports your answer.

Independent transfer

Explain why starting a 7,b 7 cannot expose the faulty swap.

Exit check

What must be preserved before a←bif the original a is needed later?

Write your reasoning before comparing with a classmate. If your teacher assigned this task, submit it through My classes to save your work and receive feedback.

Check yourself

  1. Question 1 of 2Which sequence swaps distinct numerical a and b?

    Select one answer.

  2. Question 2 of 2Starting x 2,y 5, execute x←x+y;y←x. What results?

    Select one answer.

Your practice so far

Check an answer to see its rationale.

Teacher guidance and scoring