Skip to main content
← Unit 3: Algorithms and Programming

Code Scholars Tutoring · AP Computer Science Principles

3.2 Variables and Lists

How it works

Variables store changing state; lists store ordered collections that algorithms can search, count, filter, or transform.

Track list position, current item, accumulator values, and any changes made inside the loop.

Common error

AP CSP pseudocode list indexing commonly starts with the first item at index 1.

Worked example

Track a list update using one-based AP CSP indexing.

Data / pseudocode

items <- [4, 7, 9]
items[2] <- items[1] + 1
APPEND(items, 12)
DISPLAY(items[2])

The first item is at index 1. Replacing item 2 changes the list to [4, 5, 9], and APPEND adds 12 at the end. The displayed value is 5. Do not use Java zero-based indices in this pseudocode.

Exercises

3.2.1

Start with values = [3, 6, 8]. Remove the item at position 1, then insert 5 at position 2. What is the final list?

Show solution

Removing position 1 gives [6, 8]. Inserting 5 at position 2 shifts 8 right, producing [6, 5, 8].

3.2.2

Write pseudocode that counts how many items in values are greater than 10, including an empty list.

Show solution

Solution pseudocode

count <- 0
FOR EACH value IN values {
  IF (value > 10) { count <- count + 1 }
}
DISPLAY(count)
An empty list leaves count at 0.

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic