PrepGo

AP Computer Science A Unit 2: Selection and Iteration

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: April 13, 2026

Unit Big Picture

This unit moves beyond simple, sequential programs. You will learn to control the flow of execution by making decisions and repeating actions. We will start with the logic of true and false, build conditional statements to create branching paths in your code, and then master loops to perform repetitive tasks efficiently. By the end of this unit, you will be able to write sophisticated algorithms that solve problems by intelligently managing program flow.

Core Threads

Thread 1: Control Flow

  • What it is: Directing the path of execution in a program using conditional logic and repetition. Instead of running line-by-line, the program can now make choices (if) or repeat sections of code (while, for).

  • Why it matters: Control flow is what makes software dynamic and intelligent. It allows a program to react to different inputs, handle various conditions, and process large amounts of data without redundant code.

Thread 2: Algorithm Design

  • What it is: Developing a step-by-step, repeatable process to solve a problem. This unit provides the core building blocks for translating logical steps—like "check if a number is even" or "find the first vowel in a word"—into functional Java code.

  • Why it matters: Nearly all programming is a form of algorithm design. Mastering selection and iteration enables you to move from simply writing code to architecting effective and efficient solutions to complex problems.

Key Java Concepts & Data Structures

Java Concept / Data StructureWhat It Is (1-Sentence)Why It Matters (1-Sentence)
booleanA primitive data type that can only hold the value true or false.It is the foundation of all decision-making in Java.
Relational OperatorsSymbols (>, <, ==, !=) that compare two values and produce a boolean result.They allow you to form the logical questions that control your program's flow.
Logical OperatorsOperators (&&, `
if-else StatementA control structure that executes a block of code if a condition is true, and an optional other block if it is false.This is the primary tool for creating branching paths and making decisions in code.
while LoopA control structure that repeatedly executes a block of code as long as its boolean condition remains true.It is ideal for situations where the number of repetitions is not known beforehand.
for LoopA control structure that combines initialization, a condition, and an update step to execute a block of code a specific number of times.It is perfect for definite, counter-controlled iteration, such as processing every character in a String.

Unit Concept & Syntax Bank

  • boolean expression: Any expression that evaluates to either true or false. It is the required condition for all selection and iteration statements.

  • Relational Operators: The set of operators for comparison: == (is equal to), != (is not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).

  • Logical Operators: && (logical AND), || (logical OR), and ! (logical NOT). These are used to create compound boolean expressions.

  • Short-Circuit Evaluation: The behavior of && and || where the second operand is only evaluated if necessary. For a && b, b is not evaluated if a is false.

  • De Morgan's Laws: A set of rules for transforming logical expressions. For example, !(a && b) is equivalent to !a || !b.

  • if-else if-else: A chain of conditional statements that checks conditions sequentially and executes only the first block whose condition is true.

    
    int score = 85;
    
    if (score >= 90) {
    
        System.out.println("A");
    
    } else if (score >= 80) {
    
        System.out.println("B"); // This block runs
    
    } else {
    
        System.out.println("C or lower");
    
    }
    
  • while Loop: A pre-test loop that checks its condition before each iteration. The loop body might never execute.

    
    int i = 0;
    
    while (i < 3) {
    
        System.out.println(i); // Prints 0, 1, 2
    
        i++;
    
    }
    
  • for Loop: A compact loop structure that is ideal when you know the number of iterations.

    
    for (int i = 0; i < 3; i++) {
    
        System.out.println(i); // Prints 0, 1, 2
    
    }
    
  • .equals() vs. ==: The .equals() method compares the contents of two String objects, while == checks if two String references point to the exact same object in memory. Always use .equals() for String content comparison.

Topic Navigator

Topic TitleWhat This Adds (<=10 words)
2.1: Algorithms with Selection and RepetitionIntroducing the concepts of making choices and repeating actions.
2.2: Boolean ExpressionsLearning the true/false logic that powers decisions.
2.3: if StatementsExecuting code only when a specific condition is met.
2.4: Nested if StatementsMaking decisions within other decisions for complex logic.
2.5: Compound Boolean ExpressionsCombining multiple logical checks into a single condition.
2.6: Comparing Boolean ExpressionsUnderstanding logical equivalency, like with De Morgan's Laws.
2.7: while LoopsRepeating code as long as a condition stays true.
2.8: for LoopsRepeating code a specific, predetermined number of times.
2.9: Implementing Selection and IterationApplying these structures to solve standard algorithmic problems.
2.10: Implementing String AlgorithmsUsing loops and conditionals to process and analyze Strings.
2.11: Nested IterationPlacing loops inside other loops to solve complex problems.
2.12: Informal Run-Time AnalysisBeginning to analyze and compare the efficiency of algorithms.

Exam Skills Focus

  • Code Implementation: Writing methods that use if, while, and for statements to correctly implement a given algorithm.

  • Code Analysis: Tracing the execution of code containing loops and conditionals to determine the final output or return value.

  • Application & Design: Choosing the most appropriate control structure (if, while, or for) to solve a particular problem efficiently.

Common Misconceptions & Clarifications

  • Using == to compare Strings: The == operator checks if two String variables refer to the same object in memory, not if they contain the same characters. Always use the .equals() method for comparing String content.

  • Off-by-One Errors in Loops: A loop that runs one too many or one too few times is a common bug. Pay close attention to your loop's starting value, its boolean condition (< vs. <=), and its update step.

  • Infinite Loops: A while loop whose condition never becomes false will run forever. Ensure that some action inside the loop's body will eventually cause the condition to be falsified.

Summary

This unit provides the fundamental tools for algorithmic thinking in Java. You began with boolean logic, the simple true/false foundation for all program decisions. From there, you learned to build conditional paths using if-else statements and to create efficient, repetitive processes with while and for loops. By combining and nesting these structures, you can now write powerful algorithms to process data, solve problems, and control program behavior. Mastering control flow is the critical step toward creating dynamic and useful software.