Back to homeAP® Computer Science Principles · AP Computer Science Principles · Change subject
Start for free
AP CSP · AP® Computer Science Principles
AP Computer Science Principles: Data, Algorithms, and Impact · Big Idea 3: Algorithms and Programming

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.

Three boxes show the variable score holding 40, then 50 after score gets score plus 10. 40 score ← 40 50 score ← score + 10

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.

Practice★☆☆Not started
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?

Solve

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.

Practice★★☆Not started
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
  1. a) What are the final values of x and y?
  1. b) Explain why the program does not swap the values.
  1. c) Rewrite the program using a third variable temp so that it correctly swaps them.
Solve

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

  1. Splitting into units. Ethan has 200 minutes of video. m ← 200 MOD 60 gives 20 leftover minutes. h ← (200 - m) / 60 gives 3 full hours. So 200 minutes is 3 hours and 20 minutes.

  2. Even or odd. A number is even exactly when n MOD 2 = 0.

  3. 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.

A clock face with 12 positions numbered 0 to 11, showing that 29 MOD 12 lands on 5. 0 1 2 3 4 5 6 7 8 9 10 11 29 MOD 12 = 5
Practice★☆☆Not started
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:

  1. a) 17 MOD 5
  1. b) 20 MOD 4
  1. c) 3 MOD 8
Solve
Practice★★☆Not started
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.

Solve
Practice★★☆Not started
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.

  1. a) If the clock shows 9 and he waits 20 hours, what hour does the clock show?
  1. b) What does the expression give when start + hours is exactly 24, and why?
  1. c) How can you use MOD 2 to test whether a number is even?
Solve

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.

The word computer with positions 1 to 8 under its letters; the first three letters are highlighted. com put er 123 456 78

So SUBSTRING("computer", 1, 3) is "com". And SUBSTRING("computer", 4, 2) is "pu".

Practice★☆☆Not started
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.

  1. a) What is the result of concatenating "cyber" and "space"?
  1. b) What is SUBSTRING("computer", 1, 3)?
Solve
Practice★★★Not started
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)
  1. a) What is displayed?
  1. b) How long (in characters) would the username be if num were 2026 instead of 2026 MOD 100?
  1. c) Write one line that builds the username for Priya Shah in the same style, using variables first and last.
Solve

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.

Practice★★★Not started
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

}
  1. a) Make a table showing the values of a and b after each repetition.
  1. b) What are the final values of a and b?
  1. c) Explain why you cannot predict the final result without keeping track of the order of the two assignments inside the loop.
Solve

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.

Summary

See all the exercises in this section