Skip to main content

Pilot · L02 · 50 minutes

Variables, expressions and text

Predict and trace short programs that assign, copy and update variables, evaluate arithmetic using precedence and MOD, and apply a supplied string operation exactly as defined.

Topics: 3.1 Variables and Assignments · 3.3 Mathematical Expressions · 3.4 Strings

Goals and vocabulary

By the end you can:

  • Trace a sequence of assignments and state each variable’s value after every line.
  • Explain why copying a value does not link two variables, and why an update uses the old value.
  • Distinguish an assignment from a comparison in AP pseudocode and Python.
  • Evaluate an arithmetic expression with precedence, parentheses and MOD.
  • Apply a supplied string procedure exactly as specified.
Variable
A named abstraction that holds one value at a time.
Assignment
An instruction that evaluates the right-hand side and stores the result in the variable on the left: a ← expression.
Expression
A combination of values, variables and operators that evaluates to a single value.
Precedence
The order in which operators are applied when parentheses do not say otherwise.
MOD
The remainder after whole-number division: 17 MOD 5 is 2.
String
An ordered sequence of characters, such as "Chess".
Concatenation
Joining two strings end to end into a new string.

Assignment replaces a value

A variable is a named place that holds one value at a time. The assignment statement a ← expression works in two steps: first the expression on the right is evaluated using the values variables hold right now, then the result is stored in the variable on the left, replacing whatever was there.

AP CSP pseudocode
places ← 12places ← 9DISPLAY(places)

This displays 9. The first value is not kept anywhere: the second assignment overwrote it. Python writes the same idea with an equals sign, places = 9, which is why it is easy to misread assignment as a statement that two things are equal.

Copies are not links; updates use the old value

y ← x copies the value x holds at that moment into y. After that, the two variables are independent. Changing x later does not change y.

An update such as score ← score + 1 uses the old value of score on the right, then stores the new value back into score. It is not an equation (which would be false for every number); it is an instruction.

Assignment compared with a comparison
WrittenMeaningResult
score ← score + 1Assignment: store one more than the old score in score.Changes score. Produces no true/false value.
score = score + 1Comparison in AP pseudocode: is score equal to score + 1?Always false. Changes nothing.
score == score + 1The same comparison written in Python.Always False. Changes nothing.

Worked example: tracing state line by line

Worked example

Predict the output before you read the trace. A trace table has one column per variable and one row per line, and records the value each variable holds after that line runs.

AP CSP pseudocode
a ← 3b ← aa ← a + 4b ← b * 2DISPLAY(a)DISPLAY(b)
Trace table
LineabWhat happened
13—a gets 3. b does not exist yet.
233b gets a copy of a’s value, 3.
373Right side uses the old a: 3 + 4 = 7. b is unchanged because it holds its own copy.
476Right side uses the old b: 3 × 2 = 6.
5–676Displays 7, then 6.

The common wrong prediction is “7 then 14”, which assumes b followed a when a changed. The trace shows why it does not.

The same program in Python
a = 3b = aa = a + 4b = b * 2print(a)print(b)

Arithmetic expressions and precedence

AP pseudocode supports +, −, *, / and MOD. a MOD b is the remainder when a is divided by b, so 17 MOD 5 is 2. Multiplication, division and MOD are evaluated before addition and subtraction; operators with the same precedence are evaluated left to right. Parentheses override this.

Evaluating with precedence
ExpressionStepsValue
4 + 6 * 26 * 2 = 12, then 4 + 1216
(4 + 6) * 2Parentheses first: 10, then 10 * 220
20 - 9 MOD 49 MOD 4 = 1, then 20 - 119
12 / 4 * 3Same precedence, left to right: 3, then 3 * 39

Text values and supplied operations

A string is an ordered sequence of characters, written in quotation marks: "Chess". Exam questions and specifications often define the string procedures you may use. Read the definition and apply it exactly — do not assume extra spaces or punctuation.

A supplied definition
concat(s1, s2) returns a new string: the characters of s1 followed immediately by the characters of s2.club ← "Chess"label ← concat(club, "Club")DISPLAY(label)

This displays ChessClub with no space, because the definition adds nothing between the strings. To get "Chess Club" you would write concat(club, " Club") or concat(concat(club, " "), "Club").

Numbers and text are different kinds of value. "5" is a one-character string; 5 is a number. In Python, input() always gives a string, so int(input()) is needed before doing arithmetic — you will use this in Lab P01.

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.

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 1PLANETWaiting
  2. Input 22Waiting
  3. Input 33Waiting

Ready to experiment

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

Try this: Try count 0, a count beyond the end, and start one past the last character.

Common misconceptions

  • Assignment states that two things are equal

    The belief: x ← x + 1 is impossible because no number equals itself plus one.

    What is actually true: Assignment is an instruction, not an equation: evaluate the right side with the current value, then store it.

    Check yourself: Read the line aloud as “x gets…”. What is x after it runs if x was 4?

  • Copied variables stay linked

    The belief: After y ← x, changing x also changes y.

    What is actually true: y receives a copy of the value at that moment. The two variables are independent afterwards.

    Check yourself: In your trace table, did you change a column for a variable that was not on the left of the arrow?

  • Expressions are always worked left to right

    The belief: 2 + 3 * 4 is 20.

    What is actually true: * , / and MOD come before + and −, so 2 + 3 * 4 is 14. Only operators with equal precedence go left to right.

    Check yourself: Which operator in the expression is applied first, and why?

Guided practice

Guided: predict, then trace

With a partner, write a prediction for each fragment before tracing it. Then complete a trace table and compare it with your prediction.

Fragment A
n ← 10n ← n - 4n ← n * 2DISPLAY(n)
Fragment B
p ← 5q ← pp ← 1DISPLAY(q)
Fragment C
total ← 7 + 8 MOD 3 * 2DISPLAY(total)
  1. Write your prediction for each fragment on its own line before tracing.
  2. Trace each fragment in a table with one row per line.
  3. Circle any prediction that was wrong and name the misconception that caused it.
  4. Translate Fragment B into Python and say which symbol replaces ←.

Independent practice

Independent: trace an unseen fragment

Trace the program below without running it, then answer the questions.

AP CSP pseudocode
concat(s1, s2) returns s1 followed immediately by s2.k ← 4m ← k + 3k ← m MOD 4word ← concat("Room ", "B")DISPLAY(k)DISPLAY(m)DISPLAY(word)
  1. Complete a trace table for k, m and word.
  2. State the three values displayed, in order.
  3. Explain why m does not change when the instruction k ← m MOD 4 runs.
  4. Change only the number after MOD in the instruction k ← m MOD 4 so that k ends with the value 0. Show your working.

Exit check

  1. Trace x ← 6, y ← x, x ← y + x and state the final values of x and y.
  2. Evaluate 15 - 6 / 2 MOD 4 and show the order of the steps.

Check yourself

Practice only: answers are checked in your browser and nothing is saved or sent to your teacher. Each explanation links back to the section it practises.

Lesson practice

  1. Question 1 of 4What does this program display?

    Select one answer.

    AP CSP pseudocode
    level ← 4level ← 9DISPLAY(level)
  2. Question 2 of 4What does this program display?

    Select one answer.

    AP CSP pseudocode
    x ← 5y ← xx ← 8DISPLAY(y)
  3. Question 3 of 4What does this program display?

    Select one answer.

    AP CSP pseudocode
    places ← 12places ← places - 3places ← places - 3DISPLAY(places)
  4. Question 4 of 4Which statement correctly describes the AP pseudocode line score ← score + 1?

    Select one answer.

Your practice so far

Check an answer to see its rationale.

Apply it somewhere new

  1. Question 1 of 2In this question, *, / and MOD are evaluated before + and −, and operators with equal precedence are evaluated left to right. What value does result hold?

    Select one answer.

    AP CSP pseudocode
    result ← 2 + 3 * 4 - 10 MOD 4
  2. Question 2 of 2A specification defines concat(s1, s2) as returning the characters of s1 followed immediately by the characters of s2. What is displayed?

    Select one answer.

    AP CSP pseudocode
    first ← "Ada"last ← "Lovelace"name ← concat(last, concat(", ", first))DISPLAY(name)

Your practice so far

Check an answer to see its rationale.