Variables, Expressions, and Strings
Mateo's game shows "Score: 40". He taps a button, and a second later it shows "Score: 50". The program never printed a new number by hand. Something stored 40, and something changed it. What is that something, and how does a program keep track of it? By the end of this page you will be able to trace a program and answer exactly that.
Variables and assignment
A variable is a name attached to a stored value. Think of it as a labeled box: the label is the name, and the box holds one value at a time.
The AP® pseudocode uses an arrow for assignment:
score ← 40
Read it as "score gets 40". The arrow points from the value to the name. The right side is computed first, then the result is stored in the variable on the left.
Notice that score ← score + 10 is not a math equation. It means: take the current value of score, add 10, and store the result back in score. The old 40 is gone.
A good habit is to make a trace table: one column per variable, one row after each line. It keeps you from mixing up old and new values.
Trace this program: What is displayed, and what is the final value of b?
Trace this program:
a ← 5
b ← a + 2
a ← b * 2
DISPLAY(a)
What is displayed, and what is the final value of b?
Swapping two values
Suppose x ← 3, y ← 8, and you want to swap them. Writing x ← y then y ← x fails: after the first line both hold 8, and the original 3 is lost. You need a third variable to hold the value temporarily.
Consider this program, which is meant to swap the values of x and y: a) What are the final values of x and y? b) Explain why the program does not swap the values. c)…
Consider this program, which is meant to swap the values of x and y:
x ← 3
y ← 8
x ← y
y ← x
- a) What are the final values of
xandy?
- b) Explain why the program does not swap the values.
- c) Rewrite the program using a third variable
tempso that it correctly swaps them.
Arithmetic expressions and MOD
The AP pseudocode uses +, -, *, and / for arithmetic. It also has the MOD operator: a MOD b gives the remainder when a is divided by b.
| Expression | Division | Result |
|---|---|---|
17 MOD 5 |
17 = 3 × 5 + 2 | 2 |
20 MOD 4 |
20 = 5 × 4 + 0 | 0 |
3 MOD 8 |
3 = 0 × 8 + 3 | 3 |
When the first number is smaller than the second, nothing fits, so the remainder is the first number itself.
Order of operations works as in math class: parentheses first, then *, /, and MOD (left to right), then + and -. When in doubt, add parentheses.
What MOD is good for
-
Splitting into units. Ethan has 200 minutes of video.
m ← 200 MOD 60gives 20 leftover minutes.h ← (200 - m) / 60gives 3 full hours. So 200 minutes is 3 hours and 20 minutes. -
Even or odd. A number is even exactly when
n MOD 2 = 0. -
Wrapping around. A 12-hour clock shows 0 to 11. If it shows 9 and you wait 20 hours,
(9 + 20) MOD 12=29 MOD 12= 5. When the sum is exactly 24, the result is 0, because 24 is a whole number of laps around the clock.
The MOD operator gives the remainder after division. Evaluate: a) 17 MOD 5 b) 20 MOD 4 c) 3 MOD 8
The MOD operator gives the remainder after division. Evaluate:
- a)
17 MOD 5
- b)
20 MOD 4
- c)
3 MOD 8
Evaluate each statement and give the final values. What are a, m, and h? Explain in one sentence what h and m represent.
Evaluate each statement and give the final values.
a ← (17 MOD 5) * 3 + 20 / 4
minutes ← 200
m ← minutes MOD 60
h ← (minutes - m) / 60
What are a, m, and h? Explain in one sentence what h and m represent.
A 12-hour clock shows hours 0 through 11. Ethan uses (start + hours) MOD 12 to find the time after waiting. a) If the clock shows 9 and he waits 20 hours, what hour does…
A 12-hour clock shows hours 0 through 11. Ethan uses (start + hours) MOD 12 to find the time after waiting.
- a) If the clock shows 9 and he waits 20 hours, what hour does the clock show?
- b) What does the expression give when
start + hoursis exactly 24, and why?
- c) How can you use
MOD 2to test whether a number is even?
Strings
A string is a sequence of characters in quotes: "cyber", "Go Hawks!", even "2026". Two things to know.
Concatenation joins strings end to end. Joining "cyber" and "space" gives "cyberspace". No space is added unless you put one in.
Substring takes a piece of a string. Positions start at 1, not 0. SUBSTRING(str, start, length) returns length characters starting at position start.
So SUBSTRING("computer", 1, 3) is "com". And SUBSTRING("computer", 4, 2) is "pu".
Assume string positions start at 1. SUBSTRING(str, start, length) returns length characters of str beginning at position start, and concatenation joins two strings…
Assume string positions start at 1. SUBSTRING(str, start, length) returns length characters of str beginning at position start, and concatenation joins two strings together.
- a) What is the result of concatenating
"cyber"and"space"?
- b) What is
SUBSTRING("computer", 1, 3)?
Assume CONCAT(a, b) joins two strings and automatically turns a number into text. SUBSTRING(str, start, length) starts at position 1. a) What is displayed? b) How long…
Assume CONCAT(a, b) joins two strings and automatically turns a number into text. SUBSTRING(str, start, length) starts at position 1.
name1 ← "Jayden"
name2 ← "Brooks"
user ← CONCAT(SUBSTRING(name1, 1, 1), name2)
num ← 2026 MOD 100
user ← CONCAT(user, num)
DISPLAY(user)
- a) What is displayed?
- b) How long (in characters) would the username be if
numwere2026instead of2026 MOD 100?
- c) Write one line that builds the username for Priya Shah in the same style, using variables
firstandlast.
Tracing code with a loop
Variables really matter when a program repeats. REPEAT n TIMES { … } runs the block n times. Inside, the order of assignments matters, because each line sees the values left by the previous one.
Trace this program. In the expression a + (b MOD 4), the parentheses mean the MOD is computed first. a) Make a table showing the values of a and b after each repetition.…
Trace this program. In the expression a + (b MOD 4), the parentheses mean the MOD is computed first.
a ← 2
b ← 10
REPEAT 3 TIMES
{
a ← a + (b MOD 4)
b ← b - a
}
- a) Make a table showing the values of
aandbafter each repetition.
- b) What are the final values of
aandb?
- c) Explain why you cannot predict the final result without keeping track of the order of the two assignments inside the loop.
Vuelve al principio: Mateo's game stores the score in a variable. Each tap runs something like score ← score + 10, which reads the old value, computes the new one, and replaces it.