Variables, Lists and Procedures
Read AP-style pseudocode for assignment, lists and procedures, trace how values change, and see how procedures provide abstraction in programs.
Welcome back!
Step 1 of 11Learn
Reading AP-style pseudocode
The AP CSP exam uses its own simple language, so students who learned Python, JavaScript or blocks can all read it. Its most important symbol is the arrow, which means assignment:
score ← 10
score ← score + 5
DISPLAY(score)Read score ← 10 as "score gets 10." The second line takes the current value, adds 5 and stores 15 back in score. DISPLAY shows a value on screen. Where most languages write = for assignment, this pseudocode uses the arrow precisely so nobody confuses it with "equals." In conditions, a single = means "is equal to," and ≠ means "is not equal to."
A variable holds one value at a time. When you assign a new value, the old one is gone:
a ← 3
b ← a
a ← 7After these lines, a is 7 and b is still 3. The line b ← a copied the value 3 at that moment; it didn't link the two variables forever. Tracing questions love this trap.
The best way to trace is a table: one column per variable, one row per line, writing the new value each time something changes. It looks slow, but it's how strong students avoid careless mistakes.
Step 2 of 11Tap to reveal
Trace it line by line
score ← 10
The box named score now holds 10.
score ← score + 5
Read the right side first: 10 + 5 = 15. Then store 15 in score, replacing 10.
DISPLAY(score)
Shows 15. The old value 10 is gone; a variable holds only its latest value.
The habit
Keep a small table with one column per variable and update it after every line. It catches most tracing mistakes.
Step 3 of 11Practice · Work it out
After these lines run, what is the value of x? x ← 4, y ← x + 2, x ← y * 3, y ← x - y
Step 4 of 11Learn
Lists in the AP style
A list is an ordered collection of elements stored under one name. In AP pseudocode:
colors ← ["red", "green", "blue"]
DISPLAY(colors[1])This displays red, because AP lists start at index 1. That's different from Python and JavaScript, which start at 0, so read every question carefully. Accessing an index outside the list, such as colors[4] here, is an error that stops the program.
The built-in list procedures:
LENGTH(list)returns the number of elements.APPEND(list, value)adds a value at the end.INSERT(list, i, value)puts a value at index i, shifting later elements right.REMOVE(list, i)deletes the element at index i, shifting later elements left.
colors ← ["red", "green", "blue"]
APPEND(colors, "gold") → [red, green, blue, gold]
INSERT(colors, 2, "pink") → [red, pink, green, blue, gold]
REMOVE(colors, 1) → [pink, green, blue, gold]Notice how INSERT and REMOVE change the positions of every element after them. After removing index 1, "pink" is now at index 1. Losing track of this shifting is the most common list-tracing mistake.
Lists are a form of data abstraction: one name represents many values, so code can handle 3 colors or 3,000 without changing.
Step 5 of 11Practice · Choose one
nums ← [8, 3, 5]. Then APPEND(nums, 9), INSERT(nums, 1, 4), REMOVE(nums, 3). What is nums now?
Step 6 of 11Learn
Procedures and abstraction
A procedure (called a function or method in many languages) is a named group of instructions that can take parameters and may return a value:
PROCEDURE average(a, b)
{
RETURN((a + b) / 2)
}
result ← average(80, 90)Here a and b are parameters. The values 80 and 90 passed in the call are arguments. When RETURN runs, the procedure ends and the value replaces the call, so result becomes 85.
Procedures are the most important form of procedural abstraction. Once average exists, anyone can use it knowing only what it does (its name, parameters and return value), without knowing how it does it. You use procedural abstraction constantly: when you call a sort procedure or draw a circle, you don't think about the dozens of steps inside.
Abstraction has practical benefits that the AP framework emphasizes:
- It reduces complexity, because you think about one piece at a time.
- It allows reuse: write once, call many times.
- It makes programs easier to change: fix or improve the inside of a procedure, and every caller benefits without being rewritten.
A procedure with parameters is also more general. average(a, b) works for any two numbers, while code that averages only 80 and 90 works once.
Step 7 of 11Flashcards
Procedure vocabulary
Tap a card to flip it.
Step 8 of 11Practice · Match the pairs
In PROCEDURE area(w, h) { RETURN(w * h) } and later room ← area(4, 5), match each part to its role.
Tap an answer, then tap the place where it belongs (or drag it there).
- w and h
- 4 and 5
- RETURN(w * h)
- room
Step 9 of 11Note
Step 10 of 11Practice · Code challenge
Write a procedure in JavaScript, average(list), that returns the average of a list of numbers. Return 0 for an empty list. (Remember: JavaScript list indexes start at 0, unlike AP pseudocode.)
average([80, 90])average([2, 4, 6, 8])average([7])average([])
Step 11 of 11Practice · True or false
After a ← 5, b ← a, a ← a + 1, the value of b is 6.
Lesson complete!