Advanced Placement Computer Science A focusing on Java programming and object-oriented design.
Programs need to make decisions and repeat actions. Control structures help you do just that!
if
statements let your program choose between options.
if (score >= 60) {
System.out.println("You passed!");
} else {
System.out.println("Try again!");
}
Loops repeat code several times.
for
loops are used when you know how many times you want to repeat something.for (int i = 0; i < 5; i++) {
System.out.println("Hello!");
}
while
loops repeat as long as a condition is true.int count = 0;
while (count < 3) {
System.out.println("Counting up: " + count);
count++;
}
They allow your programs to react to user input, process data, and perform tasks automatically.
Setting an alarm: If it's a school day, set the alarm for 7 AM; otherwise, sleep in!
Using an if-else to check if a user is old enough to drive.
Repeating a message five times using a for loop.
Control structures let programs make decisions and repeat actions.