A program that can’t remember anything is just a printout. Today you give the machine a memory — and meet the notation the AP exam writes it in.
The book’s version of today. Read it after you try the box model below — the picture makes the words land. Enrolled in our Runestone course? Open it from there so your progress counts.
Deeper into Programs. In 2.1 the machine could only speak
(print). Give it a place to store values and it can start to reason —
hold a score, remember a name, do math on something it was told a moment ago.
score. Inside is a
number. You can read what’s inside any time, and you can throw the old contents out and
put something new in — the label never changes, the contents do.
That is a variable, and it is the
whole idea. Here is the part worth bracing for now: the exam does not write it the way Python
does. Python writes score = 0. The AP Pseudocode writes score ← 0.
Same box, same note, same act — two spellings. From today, you’ll see both, every
time.
An assignment statement puts a value into a variable. Read it
right to left: work out the value on the right, then drop it into the box named on the
left. The = here does not mean “is equal to” the way it does in
math — it means “gets.” score = 0 is read “score
gets zero.” The exam’s arrow, ←, actually shows that
direction, which is why it’s a nicer picture of what’s happening.
New rule, and it’s the one that trips everyone: a variable can appear on both sides of an assignment. When it does, the right side is worked out first, using the old value, and only then is the box overwritten. Watch:
score = 0 score = score + 10 score = score + 5 print(score) # 15
score ← 0 score ← score + 10 score ← score + 5 DISPLAY(score) // 15
Hover or tap any line to light up its twin in the other language. They are the same program, line for line.
Line 2 reads “score gets score-plus-ten.” Python first computes the right side
with the current value (0 + 10 = 10), then stores 10 back in score. The old
value is gone. This is how a running total works, and you’ll use it in every project from
here to May.
= (or
←) is the assignment operator. What ends up inside is the
value. The exam mostly shows the arrow, so train your eye: x ← 7 is
just x = 7 wearing the exam’s uniform.
Here is the classic. You have two variables, a and b, and you want
them to trade values. The obvious way fails, and understanding why is a
guaranteed exam question. Step through it and watch the boxes.
The fix is a third box — a temp variable that holds
one value safely while the other is overwritten. Same idea, both languages:
temp = a a = b b = temp
temp ← a a ← b b ← temp
= in Python,
← in AP Pseudocode. It means “gets,” not “is equal
to.”Five questions, some in AP Pseudocode on purpose — you read both now. Pick an answer for instant feedback.
Why does the AP exam use its own pseudocode with a
← instead of just picking Python? Because a national exam can’t assume
every classroom teaches the same language. Some schools teach Java, some JavaScript, some
Python, some a block language. A shared, language-neutral notation is an abstraction
— the same idea you named in 1.7 — that lets one exam be fair across very different
classrooms.
It costs you something real: a second spelling to learn. It buys something real too: a student who moves schools, or learns a different language next year, still shares one vocabulary with everyone else. That is the recurring shape of these decisions — a cost and a benefit, landing on different people — and you’ll keep meeting it all year.