AP® Computer Science A Unit 2 flashcardsSelection and Iteration
35 cards · about 9 minutes for the whole deck
Flashcard drill
Space flips the card, arrow keys move, and keys 1 to 4 rate how well you knew it.
- Position
- 1 / 35
- Due
- 35
- Mastered
- 0 / 35
This card: New
Flip the card before you rate it.
Selection
A step where an algorithm makes a true-or-false decision about what to do next. In Java it's written with
ifstatements.Topic 2.1: Algorithms with Selection and Repetition
Repetition
Repeating steps until a goal is reached. In Java it's written with loops; iteration is another word for it.
Topic 2.1: Algorithms with Selection and Repetition
Flowchart
A diagram of an algorithm's steps. Diamonds show decisions (selection), and arrows that loop back show repetition.
Topic 2.1: Algorithms with Selection and Repetition
Relational operators
==,!=,<,>,<=and>=. They compare two values and give aboolean, likex >= 10.Topic 2.2: Boolean Expressions
==with objectsWith objects,
==checks whether two references point to the same object, not whether the objects hold the same data.Topic 2.2: Boolean Expressions
Boolean expression
An expression that evaluates to
trueorfalse, likeage >= 16. Conditions inifstatements and loops are Boolean expressions.Topic 2.2: Boolean Expressions
ifstatementOne-way selection: its body runs only when the condition is
true. Otherwise Java skips it and moves on.Topic 2.3: if Statements
if-elsestatementTwo-way selection: the
ifbody runs when the condition istrueand theelsebody runs when it'sfalse. Exactly one of them runs.Topic 2.3: if Statements
Flow of control
The order in which statements run. Normally it's top to bottom;
ifstatements and loops change it.Topic 2.3: if Statements
Nested
ifAn
ifinside anotherif. The inner condition is checked only when the outer condition istrue.Topic 2.4: Nested if Statements
Multiway selection
An
if-else-ifchain. Java checks the conditions in order and runs only the first block that'strue; a finalelseruns if none are.Topic 2.4: Nested if Statements
Order of conditions
In an
if-else-ifchain, put the narrowest test first. Testingscore >= 70beforescore >= 90means a 95 never reaches the 90 test.Topic 2.4: Nested if Statements
&&(and)a && bistrueonly when bothaandbaretrue. Use it for ranges:x >= 1 && x <= 10.Topic 2.5: Compound Boolean Expressions
||(or)a || bistruewhen at least one ofaandbistrue, including when both are.Topic 2.5: Compound Boolean Expressions
!(not)Flips a Boolean value:
!trueisfalse. Logical operators are applied in the order!, then&&, then||.Topic 2.5: Compound Boolean Expressions
Short-circuit evaluation
Java skips the right side of
&&when the left isfalse, and of||when the left istrue. Sox != 0 && 10 / x > 1never divides by 0.Topic 2.5: Compound Boolean Expressions
Equivalent Boolean expressions
Two expressions that give the same result for every possible combination of values. A truth table checks every case.
Topic 2.6: Comparing Boolean Expressions
De Morgan's laws
!(a && b)equals!a || !b, and!(a || b)equals!a && !b. Flip the operator and negate each part.Topic 2.6: Comparing Boolean Expressions
Truth table
A table listing every combination of
trueandfalsefor the variables, with the result of an expression for each row.Topic 2.6: Comparing Boolean Expressions
Comparing with
nullobj == nullorobj != nullchecks whether a reference points to an object. Do this check before calling a method on it.Topic 2.6: Comparing Boolean Expressions
whileloopChecks its condition before every pass and runs the body while it's
true. If it startsfalse, the body never runs.Topic 2.7: while Loops
Infinite loop
A loop whose condition never becomes
false, so it never ends, like forgetting to change the loop variable.Topic 2.7: while Loops
Off-by-one error
A loop that runs one time too many or one time too few, often from using
<where you needed<=, or the other way around.Topic 2.7: while Loops
forloop headerfor (init; condition; update). The init runs once, the condition is checked before each pass, and the update runs after each pass.Topic 2.8: for Loops
Loop control variable
The variable a loop uses to track its progress, like
iinfor (int i = 0; i < n; i++). If declared in the header, it only exists inside the loop.Topic 2.8: for Loops
forandwhileare interchangeableAny
forloop can be rewritten as awhileloop: put the init before the loop and the update at the end of the body.Topic 2.8: for Loops
Divisibility test
n % d == 0istruewhenddividesnevenly. For example,n % 2 == 0tests for even numbers.Topic 2.9: Implementing Selection and Iteration Algorithms
Digit extraction
n % 10gives the last digit ofn, andn /= 10drops it. Loop whilen > 0to visit every digit, right to left.Topic 2.9: Implementing Selection and Iteration Algorithms
Min/max pattern
Start the tracker at the first value (or a value that can't win), then replace it whenever a smaller or larger value appears.
Topic 2.9: Implementing Selection and Iteration Algorithms
Counter pattern
Start a count at 0 and add 1 each time the condition you're checking is met, like counting how many numbers are even.
Topic 2.9: Implementing Selection and Iteration Algorithms
String traversal
Looping through a string's indexes from 0 to
length() - 1, usings.substring(i, i + 1)to look at one character at a time.Topic 2.10: Implementing String Algorithms
Reversing a string
Loop from the last index down to 0 and add each one-letter substring to a new string. Strings can't change, so you build a new one.
Topic 2.10: Implementing String Algorithms
Nested loop
A loop inside another loop. For each pass of the outer loop, the inner loop runs all of its passes.
Topic 2.11: Nested Iteration
Statement execution count
How many times a statement runs. A statement inside two nested loops of
npasses each runsn * ntimes.Topic 2.12: Informal Run-Time Analysis
Informal run-time comparison
Comparing how much work two code segments do by counting statement executions. Nested loops multiply counts; back-to-back loops add them.
Topic 2.12: Informal Run-Time Analysis