AP Computer Science Principles · Algorithms and programming
Variables and assignments
1Variables
A variable is an abstraction for a named storage location that holds a value. In AP pseudocode, the assignment operator is an arrow:
score ← 0
name ← "Ana"
The arrow evaluates the expression on the right and stores the result in the variable on the left. A variable holds one value at a time; assigning a new value replaces the old one.
Good variable names describe their contents: totalCost rather than t.
Check yourself
After a ← 5 then a ← 9, what is the value of a?
Show the answer
9
The new value replaces the old.
2Expressions and arithmetic
An expression combines values, variables, operators and procedure calls to produce a single value. AP pseudocode operators:
+,-,*,/(normal division, so7 / 2is 3.5).MOD: the remainder, so17 MOD 5is 2.- Order of operations as in mathematics;
MODhas the same precedence as*and/.
Worked example:
a ← 10b ← a MOD 4→ 2a ← a + b * 3→ 10 + 6 = 16
Check yourself
What is 23 MOD 6?
Show the answer
5
23 = 3 × 6 + 5.
3Tracing assignments
Assignment statements run in order, and a variable's value can depend on its earlier value.
swapping two values needs a temporary variable.
first ← 4
second ← 7
temp ← first
first ← second
second ← temp
Trace:
- temp = 4.
- first = 7.
- second = 4. The values are swapped.
Without temp, writing first ← second would lose the original 4.
Trace of a swap: temp saves the first value before it is overwritten
Check yourself
a ← 3, b ← a + 2, a ← b * 2. What are a and b?
Show the answer
a = 10, b = 5
b uses a = 3; then a = 5 × 2.
4Data types and strings
Values have types: numbers, Booleans (true, false), strings (text) and lists.
String operations:
- Concatenation joins strings:
concat("hot", "dog")gives "hotdog". - A substring is part of a string.
- In AP pseudocode, string and list positions start at 1.
Different types can be represented using the same bits; the type tells the program how to interpret them.
Check yourself
Concatenating "data" and "base" produces:
Show the answer
"database"
Joined in order.
5Going deeper: tracing assignment step by step in AP pseudocode
A variable is a named place that holds one value at a time. The value can be a number, a Boolean (true or false), a string, or a list.
Assignment: a ← expression means: evaluate the right-hand side first, then store the result in the variable on the left. The old value is replaced.
It is not an equation. count ← count + 1 makes sense: take the current value, add 1, store it back.
a ← b copies the value of b into a. Afterward they are independent: changing b later does not change a.
Worked example (trace):
a ← 3
b ← 7
a ← b
b ← a + 2
DISPLAY(a)
DISPLAY(b)
- a = 3, b = 7.
a ← b: a = 7.b ← a + 2: b = 9.- Displays 7 and then 9.
Tracing method: a table with one column per variable and one row per statement. Never rely on memory.
Worked example (the order matters):
first ← 10
second ← 20
first ← second
second ← first
- After the third line: first = 20.
- The fourth line copies that 20 back: second = 20.
- Both are 20: the 10 has been lost. This does not swap.
Swapping needs a temporary variable
temp ← first
first ← second
second ← temp
- temp = 10.
- first = 20.
- second = 10. Swapped.
Arithmetic operators: +, -, *, / and MOD.
a MOD bis the remainder when a is divided by b:17 MOD 5is 2;20 MOD 4is 0;3 MOD 7is 3.- Uses:
n MOD 2 = 0tests for an even number;n MOD 10gives the last digit. - Order of operations is the usual one; MOD has the same precedence as
*and/. - On the exam reference sheet,
/gives the ordinary quotient:7 / 2is 3.5.
Worked example (evaluate): 3 + 4 * 2 MOD 3.
4 * 2= 8.8 MOD 3= 2.3 + 2= 5.
Strings: text in quotation marks. Joining strings is concatenation; a substring is part of a string.
Relational operators (=, ≠, >, <, ≥, ≤) produce a Boolean. In this pseudocode = is a comparison; the arrow is assignment.
Accumulating: a variable updated repeatedly builds up a result.
total ← 0
REPEAT 4 TIMES
{
total ← total + 5
}
total becomes 5, 10, 15, 20.
Worked example (a longer trace):
p ← 2
q ← 5
r ← p * q
p ← r - q
q ← p + q
r ← r MOD q
DISPLAY(r)
- p = 2, q = 5, r = 10.
- p = 10 − 5 = 5.
- q = 5 + 5 = 10.
- r = 10 MOD 10 = 0.
- Displays 0.
Meaningful names: score, totalCost and isFinished tell the reader what a variable is for; a and z2 do not. Good names make code easier to read, debug and maintain, which is itself a kind of documentation.
Data abstraction: a variable lets you refer to a value by name without caring about how it is stored. Using a list in place of many separate variables goes further: one name for a whole collection.
Initialization: give a variable a value before using it. Using a variable that has never been assigned is an error.
Common mistakes
- Reading
←as "equals" and expecting the two sides to stay linked. - Using values from before an assignment after it has happened.
- Trying to swap without a temporary variable.
- Evaluating left to right and ignoring precedence.
- Confusing MOD with division.
- Forgetting to initialize an accumulator before the loop.
Check yourself
What is displayed? m ← 4, then n ← m, then m ← m + 3, then DISPLAY(n).
Show the answer
4
n received a copy of the value 4; changing m afterward does not affect it.
6For top marks
- Trace each assignment in order, writing the new value of every variable that changes.
- Remember AP pseudocode uses
←for assignment and=for comparison. MODis useful for even/odd tests (n MOD 2 = 0) and cycling through values.- A variable used to count or total must be given a starting value before a loop.
to swap, always copy one value into a temporary variable first.
Check yourself
Which tests whether n is even in AP pseudocode?
Show the answer
n MOD 2 = 0
Remainder 0.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera