AP Computer Science Principles / Chapter 3
Algorithms and Programming
Cheat sheet
Values and expressions
- A variable holds one current value; that value may itself be an ordered collection. Use meaningful names and choose numbers, Booleans, strings or lists according to needed operations.
- Assignment evaluates the right side and stores a copy. Equality compares values. Save an original value before overwriting it during a swap; a later numeric assignment does not update earlier copies.
- Parentheses group expressions. Multiplication, division and MOD precede addition and subtraction; equal-precedence arithmetic proceeds left to right. Division can be fractional; MOD gives a nonnegative integer remainder smaller than its positive divisor.
- Concatenation joins strings without implicit spaces or numeric addition. A substring is a consecutive part of a string; read the API’s starting-position and endpoint/count rules.
Control flow
- Algorithms can be expressed in words, diagrams or pseudocode before implementation. Sequencing, selection and iteration combine to express their actions.
- Comparisons produce Booleans. AND requires both inputs; inclusive OR permits either or both. NOT reverses truth. NOT(p AND q) equals NOT p OR NOT q; negating OR changes it to AND.
- IF/ELSE chooses one arm. A separate later IF reads updated state and can also execute. A nested condition is reached only within its selected enclosing branch; braces determine scope.
- REPEAT n TIMES repeats the entire body n times. REPEAT UNTIL tests before the body and permits zero repetitions. A stopping condition that updates cannot reach produces an infinite loop.
- A robot’s turn changes heading in place; a forward move changes location in that heading. CAN_MOVE uses relative directions without moving. A blocked attempted move terminates execution.
Collections and search
- AP list indices run from 1 through current length. [] has no element; [""] has one empty-string element. A copied list is independent under the exam pseudocode’s abstraction.
- Indexed assignment replaces without changing length. INSERT shifts later positions right; REMOVE shifts them left; APPEND adds at the end. Re-evaluate indices after every mutation.
- Initialize aggregates once before traversing. Sum starts at zero; min/max can start from the first element of a nonempty list. Average uses the matching count. FOR EACH visits elements in order; a successful linear search can stop early.
- Binary search needs sorted order to eliminate an impossible half after each middle comparison. Doubling sorted input adds about one worst-case comparison. Sorting costs matter when deciding whether to use binary search for a few lookups.
Procedures and reusable code
- Arguments bind to parameters by position. A definition does not execute until called. RETURN supplies a value and immediately ends the call; DISPLAY produces visible output. The caller resumes after the call.
- Procedural abstraction names a reusable process; parameters generalize it. Modularity divides a program into components. Preserving inputs, return behavior and visible effects permits internal implementation changes.
- A library supplies reusable procedures; an API specifies their usage and behavior. Check formats, units, argument order, effects and reuse conditions. Tested components still need integration checks.
- Compare algorithms by all allowed inputs, results and side effects. A single counterexample disproves equivalence. Correct components can be composed incorrectly if their inputs, ordering or meanings do not connect.
Models and computational limits
- RANDOM(a,b) includes both integer endpoints, with b-a+1 equally likely values. Count favorable outcomes. A stored draw reused in a loop differs from a fresh draw per iteration; expected counts are not quotas.
- Simulations update a simplified state to investigate a phenomenon. Randomness can model variability. More runs do not repair omitted mechanisms or biased inclusion choices; compare predictions with relevant observations.
- An instance adds particular input to a general problem. Decision asks yes/no; optimization seeks a best solution. Count operations as input grows: fixed powers of n are polynomial, while exponential and factorial growth can become impractical.
- A heuristic can supply a useful feasible solution without guaranteeing optimality. A decidable problem has a correct terminating algorithm for every allowed input. Undecidable general problems lack such an algorithm; a timeout is not proof of a negative answer, and restricted cases can still be decidable.