AP Computer Science A

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

Basic Concepts

Control Structures: If, Loops, and Logic

Making Decisions and Repeating Actions

Programs often need to make choices and repeat actions. Java's control structures—if statements, loops, and logic operators—let you manage the flow of your programs.

Making Choices: If Statements

Use if to run code only under certain conditions.

if (score >= 90) {
  System.out.println("Great job! You got an A!");
} else {
  System.out.println("Keep trying!");
}

Repeating Actions: Loops

  • For Loop: Repeat a set number of times.
    for (int i = 0; i < 5; i++) {
      System.out.println("Count: " + i);
    }
    
  • While Loop: Repeat while a condition is true.
    int count = 0;
    while (count < 5) {
      System.out.println("Count: " + count);
      count++;
    }
    

Smart Logic

Combine conditions using logical operators (&&, ||, !) to make your programs smarter.

Real-World Impact

Control structures help apps make decisions: social media filters posts, games check for winning conditions, and robots follow instructions.

Best Practices

  • Keep your conditions clear and simple.
  • Watch out for infinite loops!

Examples

  • Using a loop to print all even numbers from 1 to 100.

  • Checking if a password is correct before allowing access.

In a Nutshell

Control structures let your programs make decisions and repeat tasks, making them flexible and powerful.