AP CSP – Unit 3.1: Sequencing, Selection, and Iteration

Control Structures: if-statements, loops, and nesting


1. Sequencing

Definition: Sequencing is the order in which steps are executed in a program. Code runs from top to bottom.

Key Ideas:

Example:

x = 5
y = x + 3
DISPLAY(y)

Why Sequencing Matters:


2. Selection (Decision Making)

Definition: Selection uses conditions to control which block of code runs, using if-statements.

Key Ideas:

Example:

IF(score > 70)
    DISPLAY("You passed!")
ELSE
    DISPLAY("Try again.")
END

Common Uses:


3. Iteration (Loops)

Definition: Iteration repeats a block of code multiple times using loops.

Types of Loops:

Examples:

Repeat N Times Loop:

REPEAT 5 TIMES
    DISPLAY("Hello")
END

Repeat Until Condition:

REPEAT UNTIL(answer == "yes")
    answer = INPUT()
END

Key Concepts:


4. Nesting

Definition: Nesting means placing one control structure inside another.

Why Nesting Matters:

Example:

REPEAT UNTIL(gameOver)
    IF(score > highScore)
        DISPLAY("New High Score!")
    END
END

Nested Loops Example:

FOR row FROM 1 TO 5
    FOR column FROM 1 TO 5
        DISPLAY(row, column)
    END
END

5. How These Concepts Work Together

Programs combine sequencing, selection, and iteration to create functional algorithms.

Example: Guessing Game Logic

secret = RANDOM(1,10)
guess = 0

REPEAT UNTIL(guess == secret)
    guess = INPUT("Enter a number")

    IF(guess < secret)
        DISPLAY("Too low")
    ELSE IF(guess > secret)
        DISPLAY("Too high")
    ELSE
        DISPLAY("Correct!")
    END
END

This example uses:


6. Why Control Structures Matter