BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.1
3.1 Variables and Assignments
The foundation of every code-tracing question. A variable holds one value at a time, and assignment replaces it.
What you need to know
- A variable is a named abstraction for a value stored in memory. It holds one value at a time; assigning a new value replaces the old one.
- AP pseudocode uses
a ← expressionfor assignment. The right side is evaluated first, then the result is stored in the variable on the left. - Because the right side is evaluated first,
x ← x + 1means "take the current value of x, add 1, store it back in x." - Data types matter: numbers, Booleans (true/false), strings (text), and lists. Operations behave differently depending on type.
- Assigning a variable's value to another variable copies the value at that moment. Later changes to one don't affect the other.
- Good variable names (
totalScore, nott) are a form of documentation and reduce logic errors. - Swapping two values requires a third, temporary variable:
temp ← a,a ← b,b ← temp.
Worked example
a ← 5 b ← a a ← a + 3 DISPLAY(a) DISPLAY(b)
Trace it: a = 5. b gets a copy of 5. Then a becomes 5 + 3 = 8. b is still 5 — copying happened before a changed. Output: 8 5. (DISPLAY outputs its value followed by a space.)
Exam tip: On any tracing question, draw a two-column table: variable name, current value. Update one row per line of code. Nearly every wrong answer on these questions comes from doing the update in your head and slipping.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- A variable's value can change (that's what makes it a variable) but its name and type don't. In AP pseudocode types aren't declared, but the exam still expects you to know what type a value is: 5 is a number, "5" is a string, true is a Boolean.
- Assignment is not symmetric.
a ← bchanges a and leaves b alone.b ← ais the opposite. Read the arrow: the value flows into the variable at the arrowhead's base. - The order of statements is the order of evaluation.
x ← 5thenx ← x * 2gives 10. Reversed, x would be 5 (the first line would use whatever x was before). Sequencing questions test exactly this. - Copying a value into a second variable creates an independent copy for simple values. Later changes to either don't affect the other. (Lists behave differently in some languages, but AP pseudocode questions generally treat list assignment as a copy too.)
- Swapping needs three assignments and a temporary. Two assignments (
a ← b,b ← a) leave both variables with b's original value — the first line destroyed a's value before it was saved. - Variable names should describe what the variable holds. The exam's own questions use descriptive names (
total,count,isFound), and reading them tells you the algorithm's intent before you trace.
Mistakes that cost points
- Tracing in your head. The single biggest source of wrong answers in Big Idea 3. A table with one row per statement takes 30 seconds and removes the errors.
- Reading ← as equals.
x ← x + 1isn't a false equation; it's an instruction. Evaluate the right side with the current x, store the result in x. - Assuming the copy updates. After
b ← a, changing a does not change b. Students expect b to "follow" a. It doesn't. - Botching the swap. If a question asks which code segment swaps two values, the answer uses a temp variable. Any two-line answer is wrong.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Consider the following code segment.
x ← 10 y ← x x ← 20 DISPLAY(y)What is displayed?
Show answer
Answer: A. y received a copy of x's value (10) at the time of assignment. Changing x afterward does not change y.
Q2 Which of the following code segments correctly swaps the values of variables
a and b?Show answer
Answer: B. Without a temporary variable, the first assignment overwrites one of the values before it can be copied. Option B saves a first.
Key vocabulary
- Variable
- a named storage location that holds one value at a time
- Assignment
- storing the result of an expression in a variable, written with ← in AP pseudocode
- Data type
- the kind of value a variable holds: number, Boolean, string, or list