Skip to main content

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
Saved on this device · Sign in to sync

This card: New

Something wrong with this card?

What's wrong?

Please don't include personal details.

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 if statements.

    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 a boolean, like x >= 10.

    Topic 2.2: Boolean Expressions

  • == with objects

    With 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 true or false, like age >= 16. Conditions in if statements and loops are Boolean expressions.

    Topic 2.2: Boolean Expressions

  • if statement

    One-way selection: its body runs only when the condition is true. Otherwise Java skips it and moves on.

    Topic 2.3: if Statements

  • if-else statement

    Two-way selection: the if body runs when the condition is true and the else body runs when it's false. 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; if statements and loops change it.

    Topic 2.3: if Statements

  • Nested if

    An if inside another if. The inner condition is checked only when the outer condition is true.

    Topic 2.4: Nested if Statements

  • Multiway selection

    An if-else-if chain. Java checks the conditions in order and runs only the first block that's true; a final else runs if none are.

    Topic 2.4: Nested if Statements

  • Order of conditions

    In an if-else-if chain, put the narrowest test first. Testing score >= 70 before score >= 90 means a 95 never reaches the 90 test.

    Topic 2.4: Nested if Statements

  • && (and)

    a && b is true only when both a and b are true. Use it for ranges: x >= 1 && x <= 10.

    Topic 2.5: Compound Boolean Expressions

  • || (or)

    a || b is true when at least one of a and b is true, including when both are.

    Topic 2.5: Compound Boolean Expressions

  • ! (not)

    Flips a Boolean value: !true is false. 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 is false, and of || when the left is true. So x != 0 && 10 / x > 1 never 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 true and false for the variables, with the result of an expression for each row.

    Topic 2.6: Comparing Boolean Expressions

  • Comparing with null

    obj == null or obj != null checks whether a reference points to an object. Do this check before calling a method on it.

    Topic 2.6: Comparing Boolean Expressions

  • while loop

    Checks its condition before every pass and runs the body while it's true. If it starts false, 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

  • for loop header

    for (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 i in for (int i = 0; i < n; i++). If declared in the header, it only exists inside the loop.

    Topic 2.8: for Loops

  • for and while are interchangeable

    Any for loop can be rewritten as a while loop: put the init before the loop and the update at the end of the body.

    Topic 2.8: for Loops

  • Divisibility test

    n % d == 0 is true when d divides n evenly. For example, n % 2 == 0 tests for even numbers.

    Topic 2.9: Implementing Selection and Iteration Algorithms

  • Digit extraction

    n % 10 gives the last digit of n, and n /= 10 drops it. Loop while n > 0 to 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, using s.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 n passes each runs n * n times.

    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