Unit 3: Algorithms and Programming

Variables, functions, conditionals, iteration, and lists

Unit Resources

Select a resource below to start studying.

📚Study Guide: Algorithms and Programming

Unit 3: Algorithms and Programming

Overview: This unit forms the computational core of AP Computer Science Principles by exploring how algorithms solve problems and how programming languages express those algorithms as executable code. An algorithm is a finite set of well-defined instructions that accomplish a specific task, and its efficiency is measured by how its resource requirements grow with input size. Students must understand variables as named storage locations that hold values, and the assignment operator as the mechanism for storing data. Boolean expressions, using comparison and logical operators, form the basis of decision-making in programs. Conditionals (if, if-else, nested conditionals) allow programs to execute different blocks of code based on whether certain conditions are true or false. Loops—for loops, while loops, and repeat until loops—enable repeated execution of code blocks, and students must be able to trace loop execution carefully to avoid off-by-one errors. Lists (or arrays) are ordered collections of elements accessed by index, and list traversal—processing each element sequentially—is one of the most common programming patterns. Procedures and functions encapsulate reusable code blocks, accept parameters as inputs, and may return values as outputs. This procedural abstraction allows developers to write modular, readable, and maintainable code. The unit also covers algorithm efficiency, comparing linear search (O(n)) and binary search (O(log n)), and emphasizing that algorithm choice can dramatically affect program performance as data scales. Students must practice tracing code, identifying infinite loops, and understanding variable scope.

Key Concepts

  • Algorithms: Step-by-step procedures for solving problems. Good algorithms are correct, efficient, and clear. Efficiency is often described using Big-O notation.
  • Variables and Assignment: Variables store data values. The assignment statement (x = 5) stores a value in a variable, while the equality check (x == 5) compares values.
  • Boolean Expressions: Expressions that evaluate to true or false using comparison operators (==, !=, <, >, <=, >=) and logical operators (AND, OR, NOT).
  • Conditionals: Control structures that execute different code paths based on Boolean conditions. If statements handle single conditions; if-else handles two branches; nested conditionals handle complex logic.
  • Loops and Iteration: For loops iterate a known number of times; while loops continue as long as a condition remains true; repeat until loops run until a condition becomes true.
  • Lists: Ordered collections of elements accessed by numeric index. Common operations include adding, removing, accessing, and traversing elements.
  • Procedures and Functions: Reusable code blocks that perform specific tasks. Parameters allow inputs; return statements send outputs back to the caller.
  • Algorithm Efficiency: Linear search checks each element in order (O(n)). Binary search repeatedly halves a sorted list (O(log n)). Algorithm choice significantly impacts performance at scale.

Vocabulary

  • Algorithm: A finite set of instructions to accomplish a specific task.
  • Variable: A named storage location for a value.
  • Assignment Statement: A command that stores a value in a variable.
  • Boolean Expression: An expression that evaluates to true or false.
  • Conditional Statement: A control structure that executes different code based on whether a condition is true or false.
  • Iteration: The repetition of a process or set of instructions in a program.
  • List: An ordered collection of elements.
  • Index: A numeric value used to identify the position of an element in a list.
  • Procedure: A named group of programming instructions that may have parameters and return values.
  • Parameter: A variable in a procedure definition that accepts an input value.
  • Argument: The actual value passed to a procedure parameter when the procedure is called.
  • Return Value: The value sent back by a procedure to the code that called it.
  • Linear Search: An algorithm that checks each element in a list sequentially until the target is found.
  • Binary Search: An algorithm that repeatedly divides a sorted list in half to find a target element.

Essential Structures

  • Linear Search: O(n) time; works on unsorted lists
  • Binary Search: O(log n) time; requires sorted list
  • List Index: Typically starts at 0 or 1 depending on language
  • Modulo Operator (%): Returns the remainder of division

Common Mistakes

  • Off-by-one errors in loops and list indexing, especially at the beginning or end of a list.
  • Confusing assignment (=) with equality comparison (==), leading to unintended variable changes.
  • Creating infinite loops by failing to update the loop condition variable inside the loop body.
  • Modifying a list while iterating over it, which can cause elements to be skipped or processed incorrectly.
  • Assuming binary search works on unsorted lists. Binary search requires the list to be sorted to function correctly.

AP Exam Strategies

  • Trace code manually with a specific example when uncertain about behavior. Write down variable values after each line executes.
  • For list questions, explicitly track the index and the value at that index to avoid off-by-one errors.
  • When writing about algorithms, describe both the process and the efficiency (best case, worst case, or average case).
  • Use meaningful variable names in pseudocode or descriptions to demonstrate clarity of thought.

Real-World Applications

  • Recommendation Algorithms: Netflix and Spotify use complex algorithms analyzing user behavior data to recommend movies and songs, processing millions of data points in real time.
  • Search Engines: Google uses highly optimized indexing and search algorithms to return relevant results from billions of web pages within milliseconds.
  • GPS Routing: Navigation apps like Google Maps use graph algorithms (such as Dijkstra's algorithm) to calculate the fastest route considering real-time traffic data.

Practice Quiz: Algorithms and Programming

Answer each question one at a time. Click an option to select your answer.

Question 1 of 150
Question
Loading...
Click to flip
Answer
Loading...
Click to flip back 🔀 Shuffle
1 / 41

🎥Free Video Lessons: Algorithms and Programming

Watch these unit review videos directly on our site.

AP Computer Science Principles(Full Review of all Content) - 2025 by Math Ace

AP CSP Semester Exam Review/Practice 1-24 by CalculusRCHS

AP CS Principles: Algorithms and Programming Review by Mr Comp Sci

📄Cheat Sheet: Algorithms and Programming

Quick reference for Algorithms and Programming. Print this out and review before the exam!

Unit 3 Cheat Sheet: Algorithms and Programming

  • Algorithm: Step-by-step solution to a problem
  • Variable: Named storage for data
  • Assignment: x = 5 stores value; x == 5 compares
  • Conditionals: if, if-else, nested for decisions
  • Loops: for (known iterations), while (condition-based), repeat until
  • List: Ordered collection, accessed by index
  • Procedure: Reusable block of code with parameters and optional return value
  • Linear Search: Check each element, O(n), works on any list
  • Binary Search: Divide in half, O(log n), requires sorted list
  • Modulo (%): Remainder of division
  • Trace code: Track variable values line by line

🔬Ultimate Review Packet Materials

Download official review materials for this unit.

No URP materials available for this unit yet.

Check back soon for study guides, practice questions, and review videos.

← Back to AP Computer Science Principles