AP Computer Science A

Advanced Placement Computer Science A focusing on Java programming and object-oriented design.

Basic Concepts

Control Structures

Making Decisions: If Statements

Programs need to make decisions and repeat actions. Control structures help you do just that!

If-Else

if statements let your program choose between options.

if (score >= 60) {
  System.out.println("You passed!");
} else {
  System.out.println("Try again!");
}

Loops: for and while

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++;
}

Why Use Control Structures?

They allow your programs to react to user input, process data, and perform tasks automatically.

Everyday Example

Setting an alarm: If it's a school day, set the alarm for 7 AM; otherwise, sleep in!

Examples

  • Using an if-else to check if a user is old enough to drive.

  • Repeating a message five times using a for loop.

In a Nutshell

Control structures let programs make decisions and repeat actions.