The Stacks
UNIT 2: SELECTION AND ITERATION · CHEAT SHEET

Selection and Iteration — the one-page version

Every key term and every exam tip from the 12 topics in this unit. Print it, fold it, read it on the bus.

2.1Algorithms with Selection and Repetition

Sequence
statements executed one after another
Selection
choosing a path with a conditional
Repetition
repeating with a loop; also called iteration
Tip: Before tracing, write what each variable starts as. Then execute statements strictly in order. If a question describes an algorithm in English and asks which code implements it, translate the English into initializations, a loop, and a decision — then match.

2.2Boolean Expressions

Relational operator
<, >, <=, >=, ==, != — compares two values and yields a boolean
equals()
compares object contents
Tip: Any time a question compares two Strings with ==, be suspicious. If both came from literals it may happen to be true, but the exam's intended answer is that == is not a reliable content comparison — .equals is.

2.3if Statements

One-way selection
an if statement with no else
Two-way selection
an if statement with an else
Tip: Indentation means nothing to Java. When braces are missing, exactly one statement belongs to the if. The exam writes misleadingly-indented code on purpose; read braces, not spacing.

2.4Nested if Statements

Nested if
an if statement inside the block of another if
else-if chain
a series of conditions tested in order; only the first true one runs
Dangling else
an else that pairs with the nearest unbraced if
Tip: Trace else-if chains top to bottom and stop at the first true condition. For nested ifs with no braces, an else attaches to the closest if above it — draw the braces in yourself before tracing.

2.5Compound Boolean Expressions

&& (and)
true only when both operands are true
|| (or)
true when at least one operand is true
! (not)
negates a boolean
Short-circuit evaluation
the right operand of && or || is skipped when the left operand decides the result
Tip: For "which condition is safe" questions, the null or zero check must come first in an &&. For evaluation questions, compute each relational part to true/false, then apply ! → && → ||.

2.6Comparing Boolean Expressions

Equivalent expressions
expressions with the same value for all inputs
De Morgan's laws
rules for negating && and || expressions
Truth table
a table of all input combinations and their resulting values
Tip: Apply De Morgan mechanically: flip the operator, negate each side, and flip each comparison to its boundary-inclusive opposite. Then test one or two values. Options that negate the parts but keep the same &&/|| are the standard distractor.

2.7while Loops

while loop
repeats its body as long as the condition is true; condition checked first
Infinite loop
a loop whose condition never becomes false
Sentinel
a special input value that signals a loop to stop
Tip: Always trace with a table and include a row for the final failed check — that's where students stop one iteration early or late. And check whether the condition is true at the start; "zero iterations" is a real answer choice.

2.8for Loops

for loop
a loop with initialization, condition, and update in its header
Loop variable
the counter declared in the for header; scoped to the loop
Scope
the region of code where a variable can be used
Tip: Count iterations by listing the loop variable's values: start, add the step until the condition fails. Don't compute a formula unless the step is 1. And remember: < vs <= is one full extra iteration.

2.9Implementing Selection and Iteration Algorithms

Accumulator
a variable that collects a running total or count across iterations
Digit extraction
using % 10 and / 10 to process an integer's digits
Tip: On FRQ 1, the rubric gives separate points for the loop, the condition, the update, and the return. Even if the logic isn't perfect, a correctly structured loop with the right variable initialized earns credit. Write the skeleton first, then fill in the condition.

2.10Implementing String Algorithms

Palindrome
a String that reads the same forward and backward
Substring search
checking each window of a String against a target
Tip: The loop bound for substring searching is i <= s.length() - sub.length() — get that off by one and you either miss the last match or throw an exception. Always use .equals inside the loop, never ==.

2.11Nested Iteration

Nested loop
a loop inside the body of another loop
Inner loop
the loop that runs to completion on each pass of the outer
Tip: Write the outer variable's value in the margin, then list the inner loop's values for that pass. Don't try to hold both counters in your head. For "how many times" with a dependent inner loop, write out the sum explicitly.

2.12Informal Run-Time Analysis

Statement execution count
how many times a statement runs; the exam's measure of efficiency
Run-time analysis
estimating an algorithm's work as a function of input size
Tip: Read the loop headers, not the bodies. Nested with independent bounds → multiply. Nested with dependent bound → triangular sum (n(n+1)/2). Two loops in sequence → add. A question asking "how many times is the statement executed when n = 5" wants a number, so compute it.