Skip to main content
AP CSP course

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

Variables and state

By the end you can:

  • Choose a meaningful representation for a value.
  • Distinguish a value from its variable name.
  • Explain how assignment changes current state.

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 variable lets a programme refer to a value by a name. Choose a representation that fits its use: a count is numerical, a yes/no property is Boolean, and a label such as a room code is text even when it contains digits. A meaningful name helps explain intent but does not guarantee the stored value is correct.

Assignment evaluates the right side using current values and stores that result on the left. It is not a mathematical claim that both sides remain equal forever. When one number is assigned to another variable, a later assignment to the original does not automatically change the copied value.

Worked example

A room model starts seats←12, then backup←seats, then seats←seats−3.

seats ← 12
backup ← seats
seats ← seats - 3
  1. After the first line,seats is 12.
  2. backup receives the current value 12.
  3. The final line stores 9 in seats; backup remains 12.

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: Assigning backup←seats creates a permanent link between them.

For these numerical values it copies the current value; later assignments change only their target.

Guided practice

Give the final values of seats and backup and explain their difference.

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

Independent transfer

Choose representations for a room code “007”, available places 7 and whether a booking is confirmed.

Exit check

Why does a variable name not determine a value by itself?

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 2After x←6;y←x;x←2, what isy?

    Select one answer.

  2. Question 2 of 2Which TWO representations fit the stated uses?

    Select exactly two answers (0 of 2 selected).

Your practice so far

Check an answer to see its rationale.

Teacher guidance and scoring