Control Flow: Conditionals & Loops
AP Computer Science A — if/else conditionals, boolean operators, and for/while loops in Java. Worked example plus how to avoid off-by-one errors.
Control flow decides which statements run and how many times. In AP CSA (Java) this means conditionals and loops.
Conditionals
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else {
grade = "C";
}
Boolean operators: && (and), || (or), ! (not). Comparison: ==, !=, <, >, <=, >=.
Loops
for loop — when you know how many times:
for (int i = 0; i < 5; i++) {
System.out.println(i); // prints 0,1,2,3,4
}
while loop — repeat while a condition holds:
int n = 1;
while (n <= 3) {
n = n * 2; // 2, then 4
}
Worked example
How many times does for (int i = 1; i <= 5; i++) run? Exactly 5 times (i = 1,2,3,4,5).
Exam tip
Off-by-one errors are the number-one loop mistake. Check the boundary carefully: i < 5 runs 5 times (0–4), but i <= 5 runs 6 times (0–5). On free-response questions, trace the loop with a small table of variable values before writing your answer.
Simulation interactive
Bouge les curseurs : vois le changement en temps réel.
for (int i = 0; i < 5; i++)
runs 5 times
Courte vidéo de cours
Examen blanc
Quiz d'entraînement
Teste-toi : résultats et explications immédiats.
1. How many times does for (int i = 0; i < 5; i++) run?
2. In Java, the && operator means:
3. How many times does for (int i = 0; i <= 5; i++) run?
Besoin d'aide sur ce sujet ?
Lors d'un appel de découverte gratuit de 30 minutes, nous évaluons ton niveau et construisons un plan d'étude sur mesure.
Appel de découverte gratuit