Unit 3.1 AP CSP Review

Sequencing, Selection, and Iteration

Learning Goal

By the end of this lesson, students should be able to explain and use three basic programming structures:

  1. Sequencing — code runs in order
  2. Selection — code makes decisions
  3. Iteration — code repeats
Big Idea: Sequencing, selection, and iteration are the foundation of almost every program.

1. What Is Sequencing?

Sequencing means that instructions run step by step in the order they are written.

A computer follows instructions exactly. It does not guess what the programmer meant.

Everyday Example

Making a sandwich:

  1. Get two slices of bread.
  2. Add peanut butter.
  3. Add jelly.
  4. Put the slices together.
  5. Eat the sandwich.

If the steps are out of order, the result may not make sense.

Programming Example

DISPLAY("Welcome")
DISPLAY("Enter your name")
name ← INPUT()
DISPLAY("Hello, " + name)

The program runs from top to bottom. First it displays "Welcome", then asks for a name, then stores the input, then displays a greeting.

Important Idea: The order of commands matters.
x ← 5
x ← x + 2
DISPLAY(x)

Output:

7

2. What Is Selection?

Selection means that a program makes a decision and chooses which code to run.

Selection usually uses an IF statement.

Basic Selection Format

IF condition
{
    code to run if condition is true
}

Example

score ← 85

IF score >= 70
{
    DISPLAY("Passing")
}

Output:

Passing

The message displays because 85 >= 70 is true.

If / Else Selection

An IF / ELSE statement gives the program two possible paths.

IF condition
{
    code if condition is true
}
ELSE
{
    code if condition is false
}

Example

temperature ← 95

IF temperature > 90
{
    DISPLAY("It is hot outside.")
}
ELSE
{
    DISPLAY("It is not too hot.")
}

Output:

It is hot outside.
Why Selection Matters: Selection allows programs to react differently based on data.

Examples include:

3. Boolean Expressions

Selection depends on Boolean expressions.

A Boolean expression is a statement that evaluates to either:

true

or

false

Common Comparison Operators

Operator Meaning Example
= Equal to score = 100
Not equal to score ≠ 0
> Greater than score > 90
< Less than score < 70
Greater than or equal to score ≥ 60
Less than or equal to score ≤ 100

Boolean Expression Examples

age >= 16

This is true if age is 16 or older.

password = "Cyber123"

This is true if the password exactly matches "Cyber123".

lives > 0

This is true if the player still has lives remaining.

4. What Is Iteration?

Iteration means repeating a section of code.

Iteration is also called looping.

Why Use Iteration?

Without loops, repeated tasks would require repeated code.

Instead of writing:

DISPLAY("Hello")
DISPLAY("Hello")
DISPLAY("Hello")
DISPLAY("Hello")
DISPLAY("Hello")

You can write:

REPEAT 5 TIMES
{
    DISPLAY("Hello")
}

5. Repeat Loops

A REPEAT n TIMES loop runs a set number of times.

Example

REPEAT 4 TIMES
{
    DISPLAY("AP CSP")
}

Output:

AP CSP
AP CSP
AP CSP
AP CSP
When to Use: Use a repeat loop when you know exactly how many times something should happen.

Examples:

6. Repeat Until Loops

A REPEAT UNTIL loop keeps running until a condition becomes true.

Example

REPEAT UNTIL guess = secretNumber
{
    guess ← INPUT()
}
DISPLAY("Correct!")

The loop keeps asking for guesses until the user enters the correct number.

Important Idea: A REPEAT UNTIL loop may run many times. It stops only when the condition becomes true.

7. Sequencing, Selection, and Iteration Together

Most useful programs combine all three structures.

Example Program

score ← 0

REPEAT 3 TIMES
{
    answer ← INPUT()

    IF answer = "A"
    {
        score ← score + 1
    }
}

DISPLAY(score)

What This Program Does

  1. Starts the score at 0.
  2. Repeats 3 times.
  3. Gets an answer from the user.
  4. Checks if the answer is "A".
  5. Adds 1 point if the answer is correct.
  6. Displays the final score.
Structure Where It Appears
Sequencing Instructions run from top to bottom.
Selection IF answer = "A"
Iteration REPEAT 3 TIMES

8. Tracing Code

To trace code means to carefully follow the program step by step and keep track of variable values.

AP CSP Skill: Tracing is one of the most important skills for the AP CSP exam.

Example

x ← 2
y ← 5
x ← x + y
y ← x - 1
DISPLAY(y)

Trace Table

Step x y Explanation
Start No values yet.
x ← 2 2 x gets 2.
y ← 5 2 5 y gets 5.
x ← x + y 7 5 x becomes 2 + 5.
y ← x - 1 7 6 y becomes 7 - 1.
DISPLAY(y) 7 6 Output is 6.

Output:

6

9. Tracing a Loop

Loops can be harder because the same code runs multiple times.

Example

total ← 0

REPEAT 4 TIMES
{
    total ← total + 3
}

DISPLAY(total)

Trace Table

Loop Run total Before Change total After
Start total ← 0 0
1 0 total + 3 3
2 3 total + 3 6
3 6 total + 3 9
4 9 total + 3 12

Output:

12

10. Tracing Selection

Example

points ← 80

IF points >= 90
{
    grade ← "A"
}
ELSE
{
    grade ← "Not A"
}

DISPLAY(grade)

Since 80 >= 90 is false, the program runs the ELSE section.

Output:

Not A

11. Common Student Mistakes

Mistake 1: Forgetting Code Runs in Order

DISPLAY(name)
name ← "Alex"
This is incorrect because the program tries to display name before it has a value.

Mistake 2: Confusing IF With a Loop

An IF statement checks a condition once.

A loop repeats code.

Mistake 3: Off-by-One Errors

Students often repeat code one too many or one too few times.

REPEAT 5 TIMES
{
    DISPLAY("Hello")
}

This displays "Hello" exactly 5 times, not 4 or 6.

Mistake 4: Creating an Infinite Loop

An infinite loop happens when the stopping condition is never reached.

Incorrect:

x ← 1

REPEAT UNTIL x = 10
{
    DISPLAY(x)
}

This loop never changes x, so x never becomes 10.

Correct:

x ← 1

REPEAT UNTIL x = 10
{
    DISPLAY(x)
    x ← x + 1
}

12. AP CSP Exam Connections

On the AP CSP exam, students may be asked to:

13. Quick Reference Chart

Concept Meaning Example
Sequencing Code runs in order. Step 1, Step 2, Step 3
Selection Code makes a decision. IF score >= 70
Iteration Code repeats. REPEAT 10 TIMES
Boolean Expression True or false condition. x > 5
Algorithm A step-by-step process. Recipe or program
Trace Follow code step by step. Track variable values.
Infinite Loop A loop that never stops. Condition never becomes true.

14. Practice Questions

Practice 1

What is the output?

x ← 4
x ← x + 6
DISPLAY(x)

Answer:

10

Practice 2

What is the output?

score ← 72

IF score >= 70
{
    DISPLAY("Pass")
}
ELSE
{
    DISPLAY("Fail")
}

Answer:

Pass

Practice 3

What is the output?

count ← 0

REPEAT 3 TIMES
{
    count ← count + 2
}

DISPLAY(count)

Answer:

6

Practice 4

What is the output?

x ← 5

REPEAT UNTIL x > 8
{
    x ← x + 1
}

DISPLAY(x)

Answer:

9

15. Student-Friendly Summary

Programming is built from three major control structures.

Structure Student-Friendly Meaning
Sequencing Instructions happen in order.
Selection The program makes a choice.
Iteration The program repeats instructions.

When reading AP CSP code, students should ask:

  1. What happens first?
  2. Is there a decision?
  3. Is anything repeated?
  4. What variables change?
  5. What is displayed or returned?
Final Thought: Sequencing, selection, and iteration are the building blocks of algorithms. Once students understand these three ideas, they can read, trace, and write many different types of programs.