Mastering Control Structures in AP CSP
Mastery Guide: Algorithms and Control Structures
Control structures are the building blocks of algorithms. They determine the order in which code executes, allowing programs to make decisions and repeat tasks. In AP Computer Science Principles (CSP), understanding these structures is critical for the Create Performance Task and the End-of-Course Exam.
Boolean Expressions: The Foundation of Logic
Before a computer can make a decision, it needs a question to answer. In computer science, we ask questions that have only two possible answers: True or False. These are called Boolean values.
Relational Operators
To create a Boolean expression, we often compare two values using Relational Operators.
| Operator | Meaning | AP Pseudocode Example | Result (if $x=5$) |
|---|---|---|---|
| $=$ | Equal to | $x = 5$ | true |
| $\neq$ | Not equal to | $x \neq 10$ | true |
| $>$ | Greater than | $x > 5$ | false |
| $<$ | Less than | $x < 10$ | true |
| $\geq$ | Greater than or equal to | $x \geq 5$ | true |
| $\leq$ | Less than or equal to | $x \leq 3$ | false |
Logical Operators
Complex decisions require combining multiple conditions. We use Logical Operators to glue simpler Boolean expressions together.
- NOT (Negation): Reverses the truth value.
NOT true$\rightarrow$falseNOT false$\rightarrow$true
- AND (Conjunction): Evaluating to true only if both sides are true.
- OR (Disjunction): Evaluates to true if at least one side is true.

Order of Operations
When evaluating a complex expression like NOT A OR B AND C, the standard precedence in many languages (and generally followed in logic) is:
- Parentheses $()$
- NOT
- AND
- OR
Example:
If $score = 80$ and $lives = 0$:
score > 50 \text{ AND } (lives > 0 \text{ OR } score = 100)
- Parentheses: Is $(0 > 0 \text{ OR } 80 = 100)$? $\rightarrow$ (False OR False) $\rightarrow$ False
- AND: Is $80 > 50$ AND False? $\rightarrow$ True AND False $\rightarrow$ False
Selection: Conditionals
Selection allows an algorithm to choose different paths based on the evaluation of a Boolean expression. This is accomplished using Conditional Statements.
The IF Statement
The code inside the block executes only if the condition is true.
IF (condition)
{
<block of statements>
}
The IF-ELSE Statement
This guarantees that exactly one block of code will execute. If the condition is true, the first block runs. If false, the second block (the ELSE) runs.

AP Pseudocode Example:
age <-- 15
IF (age >= 18)
{
DISPLAY("You can vote.")
}
ELSE
{
DISPLAY("You are too young to vote.")
}
Output: You are too young to vote.
Complex Logic: Nested Conditionals
Nested Conditionals connect logical paths where an IF statement is placed inside another IF or ELSE block. This is essential when checking multiple, sequential conditions.
The Logic Flow
Think of nested conditionals like a decision tree or a game of "Twenty Questions." You only ask the second question if the answer to the first question forces you down a specific path.
Pseudocode Example (Grading):
score <-- 85
IF (score >= 90)
{
grade <-- "A"
}
ELSE
{
IF (score >= 80)
{
grade <-- "B"
}
ELSE
{
grade <-- "C"
}
}
Visualizing Logic

In the example above, the code first checks if the score is $\geq 90$. Since 85 is not, it goes to the ELSE. Inside that ELSE, it checks if the score is $\geq 80$. It is, so grade becomes "B". The final ELSE (for "C") is skipped.
Repetition: Iteration (Loops)
Iteration is the repetition of a part of an algorithm until a condition is met or for a specified number of times. This helps efficient code reuse and processing lists.
Definite Loops (REPEAT n TIMES)
Use this when you know exactly how many times the code needs to run before the loop starts.
count <-- 0
REPEAT 3 TIMES
{
count <-- count + 1
DISPLAY(count)
}
Output: 1 2 3
Indefinite Loops (REPEAT UNTIL)
Use this when the number of iterations depends on the state of the program or data. The loop continues running until the Boolean expression evaluates to true.
CRITICAL DISTINCTION: In strictly text-based languages (like Python/Java),
whileloops run while a condition is true. In AP CSP block pseudocode,REPEAT UNTILruns while the condition is false. It stops when the condition becomes true.

Robot Logic Example:
REPEAT UNTIL (CAN_MOVE(forward) = false)
{
MOVE_FORWARD()
}
This robot keeps moving forward. When it hits a wall (so CAN_MOVE becomes false), the condition ... = false becomes true, and the loop stops.
Infinite Loops
An Infinite Loop occurs when the termination condition is never met.
- Example:
REPEAT UNTIL (x > 10)… but the code inside the loop never changes the value of $x$. The program effectively freezes.
Common Mistakes & Pitfalls
Confusing
=(Assignment) and=(Equality):
In many languages,=assigns a value and==compares them. In AP CSP text-based pseudocode, context matters. Usuallya <-- 5is assignment, anda = 5(inside an IF) is comparison. Always check the style guide provided on the exam.The
REPEAT UNTILLogic Trap:
Students often thinkREPEAT UNTIL (x > 5)means "Run while x > 5". It is the opposite. It means "Run while x is NOT > 5; stop as soon as x > 5".Missing Braces/Indentation in Nested Conditionals:
Tracing code logic requires strict attention to whichELSEbelongs to whichIF. Always match your opening{and closing}braces mentally.Short-Circuit Evaluation:
In anORstatement, if the first condition is true, the second condition is never checked (because the whole thing is already true). In anANDstatement, if the first is false, the second isn't checked. This matters if the second condition contains a mathematical operation or error that is skipped.
Summary Mnemonics
- AND: Like a strict parent. You must clean your room AND wash the dishes to go out. (Both must be TRUE).
- OR: Like a lenient parent. You must clean your room OR wash the dishes. (Only one needs to be TRUE).
- NOT: The rebellious teenager. Whatever you say, they say the opposite.
- Loop Logic: If it says UNTIL, you run while it's FALSE.