Algorithms and Programming Part 1: Variables, Data Types, Lists and Boolean Logic, AP CSP Big Idea 3 (35%) – Study Notes
offline

Source: AP Computer Science Principles Course Framework

Tags: variable, data type, integer, string, boolean, list, array, index, assignment operator, global variable, local variable, concatenation, substring, relational operators, AND, OR, NOT, nested conditional, IF, ELSE, append, insert, remove, linear search, binary search

Difficulty: Intermediate Prerequisites: Big Idea 1 (error types, program structure). Familiarity with basic algebra helps.


Big Picture

Big Idea 3 is the largest section of the AP exam at 35%. It covers everything about how programs actually work: variables, data types, conditionals, lists, loops, procedures, algorithms, and efficiency. This first set of notes handles variables, data types (integers, strings, Booleans, lists), Boolean logic, conditionals, and list operations. If you can trace through pseudocode that manipulates variables and lists, evaluate Boolean expressions, and explain how a binary search works, you are covering the most heavily tested material on the entire exam. The AP exam uses its own pseudocode, where list indices start at 1 (not 0).


TL;DR

Variables hold one value at a time and can be reassigned. The four key data types are integers, strings, Booleans, and lists. Boolean expressions (using AND, OR, NOT) drive conditionals (IF/ELSE). Lists store ordered sequences of elements indexed starting at 1, and you need to know how to access, insert, append, remove, and loop through them. Binary search is faster than linear search but requires a sorted list.


Key Terms

Variable

A placeholder in your program for a value, usually represented by letters or words. Each variable can hold only one data value at a time. It can be reassigned.

Assignment operator

The symbol used to store a value in a variable. In AP pseudocode: a ← expression evaluates the expression and assigns the result to a.

Global variable

A variable declared outside of any event or function. It can be used anywhere in the program. Best practice: create variables once, at the top of the code, outside any event or function.

Local variable

A variable declared inside an event or function. It exists only within that block and is deleted when the block finishes executing. In simple terms, it is temporary and invisible to the rest of the program.

Integer

A whole number, positive or negative. Example: 5, -12, 0.

String

A sequence of characters enclosed in quotation marks. Example: 'We are passing AP CSP'. Think of it as text data.

Substring

A portion of an existing string. The Slice(str, start, length) function returns a substring starting at position start with the given length.

Concatenation

Joining two strings (or a string and a number) using the + operator. Example: 'sun' + 'rise' produces 'sunrise'.

List (array)

An ordered sequence of elements. Example: foodList = ["fish", "chicken", "beef"]. Lists can store multiple related values under a single variable name.

Element

An individual value in a list. Each element is assigned an index value.

Index

The position number of an element in a list. In AP pseudocode, index numbers start at 1 (not 0).

Boolean

A data type that can only represent two values: true or false. Computers use Booleans to make decisions inside conditionals.

Relational operators

Symbols used to compare two values: =, ≠, >, <, ≥, ≤. A comparison using these evaluates to a Boolean value (true or false).

NOT operator (!)

Reverses a Boolean value. NOT true = false. NOT false = true.

AND operator (&&)

Combines two conditions. Evaluates to true only if both conditions are true. Any other combination gives false.

OR operator (||)

Combines two conditions. Evaluates to true if at least one condition is true. Only false OR false gives false.

Nested conditional

A conditional statement placed inside another conditional statement. Used when decisions depend on multiple layered conditions.

Linear search (sequential search)

Checks each element of a list in order, one by one, until the target value is found or all elements have been checked. Works on any list, sorted or unsorted.

Binary search

Starts at the middle of a sorted list and eliminates half the remaining data with each comparison. Repeats until the target is found or the search space is empty. Much faster than linear search, but the list must be sorted first.

LENGTH(aList)

Returns the number of elements in a list.

APPEND(aList, value)

Adds a value to the end of a list, increasing its length by 1.

INSERT(aList, i, value)

Inserts a value at index i, shifting all elements at index i and above one position to the right. The list grows by 1.

REMOVE(aList, i)

Removes the element at index i and shifts all elements above it one position to the left. The list shrinks by 1.


Core Content

Variables and Assignment

  • A variable holds one value at a time

  • When you reassign a variable, the old value is gone:

    • Animal = "cat" then Animal = "dog" means Animal is now "dog"

  • Variables are case-sensitive: a and A are different

  • Use global variables (declared at the top, outside events and functions) to avoid scope issues

  • Local variables exist only inside the event or function where they are created

Data Types

  • Integers: whole numbers (5, -3, 0)

  • Strings: text in quotation marks ('hello')

    • + concatenates: 'sun' + 'rise' = 'sunrise'

    • \n creates a new line

    • Slice(str, start, length) extracts a substring

  • Booleans: true or false, used in conditionals

  • Lists: ordered collections of elements, indexed from 1 in AP pseudocode

Boolean Logic and Conditionals

  • Relational operators (=, ≠, >, <, ≥, ≤) produce Boolean values

  • NOT: flips true to false and vice versa

  • AND: true only when both conditions are true

    • true AND false = false

    • true AND true = true

    • false AND false = false

  • OR: true when at least one condition is true

    • true OR false = true

    • true OR true = true

    • false OR false = false

  • IF statement: executes a block of code only when the condition is true

  • IF/ELSE: executes one block when true, a different block when false

  • Nested conditionals: an IF/ELSE inside another IF/ELSE, used for multi-layered decisions

    • Example: if age = 40, ticket = 20; else, check if age = 20 for ticket = 30; otherwise ticket = 25

List Operations

  • Access by index: aList[i] returns the element at position i (first element is at index 1)

  • Assign to variable: x ← aList[i] stores the element at index i in x

  • Assign to element: aList[i] ← x puts the value of x into position i

  • Copy between positions: aList[i] ← aList[j] copies the value at index j into index i

  • INSERT(aList, i, value): inserts at index i, shifts everything at i and above to the right, list grows by 1

  • APPEND(aList, value): adds to the end of the list, list grows by 1

  • REMOVE(aList, i): removes element at index i, shifts everything above it to the left, list shrinks by 1

  • LENGTH(aList): returns the number of elements

  • FOR EACH item IN aList: loops through every element in order

Searching

  • Linear search: check each element one by one from the beginning. Works on any list. Slow for large lists.

  • Binary search: requires a sorted list. Check the middle, eliminate half, repeat.

    • Example: searching for 12 in [1, 1, 2, 3, 3, 4, 5, 7, 9, 11, 12]

    • Middle is 4; 12 > 4, so discard the lower half

    • Remaining: [5, 7, 9, 11, 12]; middle is 9; 12 > 9, discard lower half

    • Continue narrowing until 12 is found

  • Binary search is more efficient than linear search


Formulas / Key References

  • AP pseudocode list index starts at 1, not 0

  • a ← expression is the assignment operator

  • a = b in a condition tests equality (not assignment)

  • RANDOM(a, b) generates a random integer from a to b, inclusive


Real-World Applications

  • Search engines use variations of binary search to find results quickly across massive sorted indexes. Without efficient search algorithms, returning results in milliseconds would be impossible.

  • Variables and lists are the building blocks of every app you use, from a contacts list on your phone (a list of strings) to a game's score counter (an integer variable that gets reassigned each round).


Common Misconceptions

  • Students often forget that AP pseudocode lists start at index 1, not 0. If a list has 3 elements, valid indices are 1, 2, and 3.

  • Students confuse the assignment operator with equality. a ← 5 stores 5 in a. a = 5 inside a condition checks whether a equals 5.

  • Students think INSERT replaces the element at that index. It does not. It shifts existing elements to the right and adds the new value.

  • Students assume binary search works on unsorted lists. It does not. The list must be sorted first.


Why It Matters / Exam Flags

  • ⚠️ Tracing through pseudocode that manipulates variables and lists is one of the most common question types. Practice this extensively.

  • ⚠️ Know the truth tables for AND, OR, and NOT. These appear in multiple-choice questions regularly.

  • ⚠️ Expect questions where you must determine the output of nested conditionals.

  • ⚠️ List indices start at 1 in AP pseudocode. This trips students up constantly.

  • ⚠️ Binary search vs. linear search: know the requirements, the process, and why binary is more efficient.

  • ⚠️ Global vs. local variables: expect questions about variable scope and what happens when a local variable is used outside its block.


Quick Self-Test

  1. True or False: A variable can hold multiple values at the same time.

  1. Fill in the blank: In AP pseudocode, the first element of a list is at index __________.

  1. True or False: true AND false evaluates to true.

  1. Fill in the blank: A search that requires the list to be sorted is called a __________ search.

  1. True or False: APPEND(myList, "hello") adds "hello" to the beginning of the list.

Answers: 1. False (one value at a time). 2. 1. 3. False (AND requires both to be true). 4. Binary. 5. False (APPEND adds to the end).


Practice Q&A

Q: Given nums = [10, 20, 30, 40], what is the value of nums after executing INSERT(nums, 2, 15)?

A: [10, 15, 20, 30, 40]. The value 15 is inserted at index 2, and everything from index 2 onward shifts one position to the right.

Q: What is the output of this code? a ← 10 b ← 20 a ← b DISPLAY(a)

A: 20. The third line assigns the value of b (which is 20) to a, overwriting the original value of 10.

Q: Evaluate: NOT (true AND false) OR true

A: True. true AND false = false. NOT false = true. true OR true = true.

Q: A list has 1,000 sorted elements. What is the maximum number of comparisons a binary search needs to find a value (or determine it is not present)?

A: About 10. Binary search halves the list each time, so the maximum comparisons is roughly log base 2 of 1,000, which is approximately 10 (since 2^10 = 1,024).

Q: What is the difference between a global variable and a local variable?

A: A global variable is declared outside any event or function and can be accessed anywhere in the program. A local variable is declared inside an event or function, exists only while that block runs, and cannot be accessed outside it.


Connections to Other Topics

  • Lists and data abstraction here build directly on the concept of abstraction from Big Idea 2 (Data).

  • Boolean logic connects to Big Idea 4 (Computer Systems and Networks), where binary values underpin everything from IP addresses to packet headers.

  • Searching algorithms connect to Big Idea 3 Part 2, where algorithm efficiency and reasonable vs. unreasonable time are discussed.


Related Terms / Search Tags

variable, assignment operator, global variable, local variable, data type, integer, string, substring, concatenation, Boolean, true, false, list, array, element, index, relational operators, NOT operator, AND operator, OR operator, conditional, IF statement, ELSE, nested conditional, linear search, sequential search, binary search, APPEND, INSERT, REMOVE, LENGTH, FOR EACH, AP CSP, AP Computer Science Principles, Big Idea 3, algorithms and programming