UnderGraduate Classes — Enroll Now APCSA,AP Physics & APCSP Classes Starting September/October Learn Python, Java & Web Design AP Cybersecurity — New Course Expert-Led · Small Batches · Hands-On Projects Contact: oggntech@gmail.com UnderGraduate Classes — Enroll Now APCSA,AP Physics & APCSP Classes Starting September/October Learn Python, Java & Web Design AP Cybersecurity — New Course Expert-Led · Small Batches · Hands-On Projects Contact: oggntech@gmail.com
Study Notes

Big Idea 3 — Algorithms and Programming

The heart of AP CSP: variables, lists, conditionals, loops, procedures, libraries, simulations, algorithm efficiency, and the limits of computing. This Big Idea is worth 30–35% of the AP exam and supports the Create Performance Task. Code uses AP exam pseudocode (lists start at index 1). Each section ends with a short practice set — click "Show answer" to check yourself.

Big Idea 2 — Data

How computers represent data with bits, how data is shrunk for storage and transmission, and how people and programs turn raw data into useful information. This Big Idea is worth 17–22% of the AP exam. Each section ends with a short practice set — click "Show answer" to check yourself.

Big Idea 1 — Creative Development

How computing innovations are made: working with others, understanding what a program does and why, following a design process, and finding and fixing mistakes. This Big Idea is worth 10–13% of the AP exam and also underpins the Create Performance Task. Each section ends with a short practice set — click "Show answer" to check yourself.

3.1

Variables and Assignments

A variable is an abstraction inside a program that holds a single value and has a name. The value can change as the program runs. An assignment evaluates the expression on the right, then stores the result in the variable on the left (written ← in AP pseudocode).

score ← 10
score ← score + 5      // evaluate 10 + 5, store 15 in score
DISPLAY(score)         // 15
  • A variable holds only its most recent value; assigning a new value replaces the old one.
  • Common data types: integer, real number, string, Boolean.
  • Use meaningful names (totalCost not x) so code is easier to read.
  • Swapping two variables needs a temporary variable: temp ← a, a ← b, b ← temp.
Careful: b ← a copies the current value of a into b. Later changes to a do not change b.
Practice

1. After a ← 3, b ← a, a ← 5, what are a and b?

Show answer
a = 5 and b = 3. b kept the value copied earlier.

2. What does x ← x + 1 do?

Show answer
It adds 1 to x's current value and stores the result back in x (an increment).
3.2

Data Abstraction

Data abstraction hides the details of how data is stored so programmers can work with a simpler idea. A list is the main example: it stores many values under a single name.

// Without a list: score1, score2, score3 ... (hard to scale)
scores ← [82, 91, 77, 68]     // one name, many values
  • Lists manage complexity — you don't need a separate variable for every item.
  • Code using a list can handle any number of items without being rewritten, so it is easier to develop and maintain.
  • Naming a collection (like scores) gives it meaning, which makes the program easier to read.
Create Task: you must explain how your list manages complexity — and what would be harder to do without it.
Practice

1. Name one way a list manages program complexity.

Show answer
It replaces many separate variables with one name, so the program can handle any number of items with the same code.

2. A program tracks 500 student names. Why is a list better than 500 variables?

Show answer
A list is shorter, easier to read, and can be processed with a loop; adding or removing students needs no new variables.
3.3

Mathematical Expressions

Expressions combine values, variables, and operators. AP pseudocode uses + − * / and MOD.

OperatorMeaningExample
+ − *Add, subtract, multiply2 + 3 * 4 → 14
/Division10 / 4 → 2.5
MODRemainder after division17 MOD 5 → 2
  • Order of operations follows normal math: parentheses, then * / MOD, then + −, left to right.
  • n MOD 2 = 0 tests whether n is even.
  • MOD is also useful for wrap-around, like a clock or cycling through list positions.
Tip: a MOD b is always between 0 and b − 1 (for positive numbers).
Practice

1. Evaluate (7 + 2) MOD 4.

Show answer
9 MOD 4 = 1.

2. Evaluate 2 + 3 * 4 − 6 / 2.

Show answer
3 * 4 = 12 and 6 / 2 = 3, so 2 + 12 − 3 = 11.
3.4

Strings

A string is an ordered sequence of characters (letters, digits, symbols, spaces). Strings are written in quotes.

  • Length — the number of characters, including spaces.
  • Concatenation — joining two strings end to end: "Sun" and "shine" become "Sunshine".
  • Substring — a part of an existing string: "com" is a substring of "computer".
  • String operations are available as procedures in most languages, though the exact names differ.
first ← "Ada"
last  ← "Lovelace"
full  ← first + " " + last     // "Ada Lovelace" (concatenation)
Watch out: "5" + "3" as strings gives "53", not 8.
Practice

1. What is the length of "Hi there"?

Show answer
8 — the space counts as a character.

2. Concatenate "Data" and "base".

Show answer
"Database"
3.5

Boolean Expressions

A Boolean value is true or false. Boolean expressions use relational operators and logical operators.

TypeOperators
Relational= ≠ > < ≥ ≤
LogicalNOT, AND, OR
aba AND ba OR bNOT a
truetruetruetruefalse
truefalsefalsetruefalse
falsetruefalsetruetrue
falsefalsefalsefalsetrue
Remember: AND needs both sides true; OR needs at least one true.
Practice

1. If a = 5 and b = 10, evaluate (a < b) AND (b = 10).

Show answer
true — both parts are true.

2. Evaluate NOT (a > b) OR (b < 5) when a = 5, b = 10.

Show answer
a > b is false, so NOT gives true; true OR false = true.
3.6

Conditionals

Selection lets a program choose which statements to run based on a Boolean condition. AP pseudocode uses IF and an optional ELSE.

IF (temp > 30)
{
    DISPLAY("Hot")
}
ELSE
{
    DISPLAY("Comfortable")
}
  • If the condition is true, the IF block runs; otherwise the ELSE block runs (if present).
  • Without an ELSE, nothing happens when the condition is false.
Exam tip: trace the condition with the actual values — check which branch runs.
Practice

1. What is displayed if temp ← 25 in the code above?

Show answer
"Comfortable" — 25 > 30 is false, so the ELSE block runs.

2. Write a conditional that displays "Even" if n is even.

Show answer
IF (n MOD 2 = 0) { DISPLAY("Even") }
3.7

Nested Conditionals

A nested conditional places one conditional inside another, allowing more than two outcomes. AP pseudocode has no "else if", so extra choices go inside ELSE.

IF (grade ≥ 90)
{
    DISPLAY("A")
}
ELSE
{
    IF (grade ≥ 80)
    {
        DISPLAY("B")
    }
    ELSE
    {
        DISPLAY("C or below")
    }
}
  • The inner conditional only runs if the outer branch containing it is taken.
  • Order matters: check the strictest condition first.
Trace it: for grade = 85, 85 ≥ 90 is false → go to ELSE → 85 ≥ 80 is true → "B".
Practice

1. What is displayed for grade ← 95?

Show answer
"A" — the first condition is true.

2. What is displayed for grade ← 70?

Show answer
"C or below" — both conditions are false.
3.8

Iteration

Iteration repeats a block of statements. AP pseudocode has three forms:

FormRepeats
REPEAT n TIMESA fixed number of times
REPEAT UNTIL (condition)Until the condition becomes true (checked before each pass)
FOR EACH item IN listOnce for every element of a list
i ← 1
sum ← 0
REPEAT UNTIL (i > 4)
{
    sum ← sum + i
    i ← i + 1
}
DISPLAY(sum)      // 10
  • If the REPEAT UNTIL condition is true at the start, the loop body never runs.
  • If the condition never becomes true, you get an infinite loop.
Remember: REPEAT UNTIL stops when the condition is true — the opposite of a typical while loop.
Practice

1. x ← 5, then REPEAT UNTIL (x ≥ 20) { x ← x + 5 }. How many times does the body run?

Show answer
3 times: x goes 5 → 10 → 15 → 20, and then the condition is true.

2. What happens if a REPEAT UNTIL condition never becomes true?

Show answer
An infinite loop — the program never stops.
3.9

Developing Algorithms

An algorithm is a finite set of instructions that accomplishes a task. Every algorithm is built from sequencing, selection, and iteration.

  • Algorithms can be described in natural language, flowcharts, or pseudocode, and then coded in any language.
  • Different algorithms can solve the same problem, and they can differ in efficiency.
  • New algorithms can be built by combining or modifying existing ones.

Common patterns to know: finding the maximum, summing/averaging, counting items that meet a condition, and linear search.

max ← nums[1]
FOR EACH n IN nums
{
    IF (n > max)
    {
        max ← n
    }
}
DISPLAY(max)
Tip: start the max tracker at the first element, not 0, in case all values are negative.
Practice

1. Name the three building blocks of every algorithm.

Show answer
Sequencing, selection, and iteration.

2. Write pseudocode to count how many numbers in nums are above 50.

Show answer
count ← 0; FOR EACH n IN nums { IF (n > 50) { count ← count + 1 } }
3.10

Lists

A list is an ordered sequence of elements. Each element has an index. In AP pseudocode, indexes start at 1.

OperationEffect
list[i]The element at index i
list[i] ← valueReplace the element at i
INSERT(list, i, value)Add at i; later elements shift right
APPEND(list, value)Add at the end
REMOVE(list, i)Delete element at i; later elements shift left
LENGTH(list)Number of elements
  • Traversal visits each element — with FOR EACH or a loop over indexes.
  • Linear (sequential) search checks elements one by one from the start; it works on any list, sorted or not.
  • Using an index outside 1 to LENGTH(list) causes an error.
Careful: AP lists start at index 1, but many real languages (Python, JavaScript, Java) start at 0.
Practice

1. list ← [10, 20, 30], then INSERT(list, 2, 15). What is list now?

Show answer
[10, 15, 20, 30]

2. Then REMOVE(list, 1). What is list now?

Show answer
[15, 20, 30]
3.11

Binary Search

Binary search finds a target in a sorted list by repeatedly checking the middle element and discarding the half that can't contain the target.

  1. Look at the middle element.
  2. If it equals the target, done.
  3. If the target is smaller, keep the lower half; if larger, keep the upper half.
  4. Repeat on the remaining half until found or empty.
  • The list must be sorted first.
  • Each step halves the search space, so it needs roughly log2 n steps — a list of 1024 items takes about 10 steps.
  • For large sorted lists it is much faster than linear search, which may check all n elements.
Compare: linear search → any list, up to n checks. Binary search → sorted list only, about log2 n checks.
Practice

1. Can binary search be used on [8, 3, 5, 1]?

Show answer
No — the list isn't sorted.

2. About how many comparisons does binary search need (worst case) for 1,024 sorted items?

Show answer
About 10, since 210 = 1024.
3.12

Calling Procedures

A procedure is a named group of programming instructions (also called a method or function). You call it to run its code.

  • Parameters are the input variables in the procedure's definition; arguments are the actual values passed when calling.
  • A procedure may return a value (RETURN), ending the procedure and handing a result back.
  • When called, the program jumps to the procedure, runs it, then continues after the call.
PROCEDURE doubleIt(x)
{
    RETURN (x * 2)
}

result ← doubleIt(7)     // 7 is the argument → result is 14
Terms: parameter = in the definition; argument = in the call.
Practice

1. What does doubleIt(10) return?

Show answer
20

2. In PROCEDURE greet(name), is name a parameter or an argument?

Show answer
A parameter.
3.13

Developing Procedures

Procedural abstraction lets you use a procedure without knowing how it works inside. It supports modularity: breaking a big program into smaller reusable parts.

  • Procedures reduce repeated code — write once, call many times.
  • Parameters generalize a procedure so it works with different inputs.
  • Programmers can change the internal code of a procedure without changing the code that calls it, as long as it still does the same job.
  • Good documentation states what a procedure does, its parameters, and what it returns.
  • Procedures make programs easier to debug, test, and maintain.
PROCEDURE average(a, b, c)
{
    RETURN ((a + b + c) / 3)
}
Create Task: your program must include a student-developed procedure with at least one parameter, sequencing, selection and iteration.
Practice

1. How does a procedure reduce repetition?

Show answer
The same code is written once and called wherever needed instead of being copied.

2. What is procedural abstraction?

Show answer
Using a procedure by knowing what it does, without needing to know how it does it.
3.14

Libraries

A library is a collection of procedures that can be used in programs. An API (application programming interface) describes how to use those procedures — their names, parameters, and what they return.

  • Libraries save time because the code already exists and has been tested.
  • You need to read the documentation to use a library correctly.
  • Libraries and APIs let you build complex programs (graphics, maps, data analysis) quickly.
  • Give credit and follow licensing when using other people's code.
Remember: an API tells you how to use the procedures — not how they are written inside.
Practice

1. What is the main benefit of using a library?

Show answer
It reuses existing, tested code so programs are built faster and with fewer errors.

2. What does API documentation tell a programmer?

Show answer
What each procedure does, the parameters it needs, and the value it returns.
3.15

Random Values

Random values make a program behave differently each time. In AP pseudocode, RANDOM(a, b) returns an integer from a to b, inclusive, with each value equally likely.

roll ← RANDOM(1, 6)       // 1, 2, 3, 4, 5 or 6
IF (RANDOM(1, 2) = 1)
{
    DISPLAY("Heads")
}
ELSE
{
    DISPLAY("Tails")
}
  • Because the result is unpredictable, the same program can produce different output on different runs.
  • Used in games, simulations, and testing.
Careful: RANDOM(1, 10) includes both 1 and 10 — 10 possible values.
Practice

1. How many possible outcomes does RANDOM(3, 7) have?

Show answer
5 (3, 4, 5, 6, 7).

2. How could you simulate rolling a 10-sided die?

Show answer
RANDOM(1, 10)
3.16

Simulations

A simulation is a program model of a real-world object or event. It is an abstraction that includes some details and leaves out others.

  • Used when real experiments are too dangerous, slow, expensive, or impossible (e.g. crash tests, weather, disease spread).
  • Often use random values to mimic real unpredictability.
  • Allow testing many scenarios by changing variables.
  • Limitations: results depend on the simplifications and assumptions chosen, so a simulation may contain bias or miss real-world factors.
Exam tip: a simulation never perfectly matches reality; always mention what was simplified.
Practice

1. Why might engineers use a simulation instead of a real crash test?

Show answer
It is safer, cheaper, and faster, and many scenarios can be tested repeatedly.

2. Give one limitation of simulations.

Show answer
They simplify reality, so results may be inaccurate or biased if important details are left out.
3.17

Algorithmic Efficiency

Efficiency describes how many steps (or how much time or memory) an algorithm needs as the input size n grows.

GrowthExamplesTime
Constant, linear (n), square (n²), cubic (n³)Fixed step, linear search, nested loopsReasonable (polynomial)
Exponential (2n), factorial (n!)Trying every combinationUnreasonable — too slow for large n
  • A heuristic is an approach that finds a good-enough answer when finding the best one takes too long.
  • A decision problem has a yes/no answer; an optimization problem asks for the best solution.
  • Efficiency is often judged by counting steps, not by timing on one computer.
Remember: polynomial = reasonable; exponential or worse = unreasonable.
Practice

1. Is an algorithm that takes 2n steps reasonable for large n?

Show answer
No — exponential growth becomes impractical very quickly.

2. When is a heuristic useful?

Show answer
When finding the exact best answer would take unreasonable time; a heuristic gives a good approximate answer faster.
3.18

Undecidable Problems

Some problems cannot be solved by any algorithm for every input.

  • A decidable problem has an algorithm that always gives a correct yes/no answer for every input.
  • An undecidable problem has no such algorithm — no program can solve it for all possible inputs.
  • Famous example: the halting problem — deciding whether any given program will eventually stop or run forever.
  • An undecidable problem may still be solvable for some specific inputs.
Key idea: undecidable doesn't mean "hard" — it means impossible to solve in general with an algorithm.
Practice

1. What does it mean for a problem to be undecidable?

Show answer
No algorithm can correctly solve it for every possible input.

2. Give an example of an undecidable problem.

Show answer
The halting problem — determining whether any program will halt or loop forever.