AP Computer Science A

Unit 2: Selection and Iteration

Topic 2.1: Algorithms with Selection and Repetition

Learning Objective 2.1.A:

Represent patterns and algorithms that involve selection and repetition found in everyday life using written language or diagrams.

The Three Building Blocks of Algorithms

Sequencing

Instructions executed in order, one after another

Selection

Making choices based on conditions (if-then-else)

Repetition

Repeating actions until a goal is reached (loops)

Essential Knowledge 2.1.A.1

The building blocks of algorithms include sequencing, selection, and repetition.

Selection: Making Decisions

What is Selection?

Selection occurs when a choice of how the execution of an algorithm will proceed is based on a true or false decision.

Real-World Example: Getting Dressed

Check the weather
Is it cold outside?
YES: Wear a jacket
NO: Wear a t-shirt

Essential Knowledge 2.1.A.3

Selection occurs when a choice of how the execution of an algorithm will proceed is based on a true or false decision.

Selection in Java Code

If-Else Statements

if (temperature < 60) {     System.out.println("Wear a jacket!"); } else {     System.out.println("A t-shirt is fine."); }

Multi-way Selection

if (grade >= 90) {     System.out.println("A"); } else if (grade >= 80) {     System.out.println("B"); } else if (grade >= 70) {     System.out.println("C"); } else {     System.out.println("Try harder next time!"); }

Repetition: Repeating Actions

What is Repetition?

Repetition is when a process repeats itself until a desired outcome is reached.

Real-World Example: Making Coffee

Start making coffee
Is the coffee strong enough?
NO: Add more coffee grounds
↑ (Repeat)
YES: Coffee is ready!

Essential Knowledge 2.1.A.4

Repetition is when a process repeats itself until a desired outcome is reached.

Repetition in Java Code

While Loops

int count = 1; while (count <= 5) {     System.out.println("Count: " + count);     count++; }

For Loops

for (int i = 1; i <= 5; i++) {     System.out.println("Number: " + i); }

Enhanced For Loops (For Arrays/Collections)

String[] names = {"Alice", "Bob", "Charlie"}; for (String name : names) {     System.out.println("Hello, " + name); }

Combining the Building Blocks

Order Matters!

The order in which sequencing, selection, and repetition are used contributes to the outcome of the algorithm.

Example: Guessing Game Algorithm

import java.util.Scanner; public class GuessingGame {     public static void main(String[] args) {         Scanner input = new Scanner(System.in);         int secret = (int)(Math.random() * 10) + 1;         int guess = 0;                  while (guess != secret) {             System.out.print("Guess a number (1-10): ");             guess = input.nextInt();                          if (guess < secret) {                 System.out.println("Too low!");             } else if (guess > secret) {                 System.out.println("Too high!");             }         }         System.out.println("Congratulations!");     } }

Algorithms in Everyday Life

Example 1: Getting Ready for School

1. Wake up (Sequencing) 2. Check the time (Sequencing) 3. IF time < 7:00 AM (Selection)     Go back to sleep for 10 minutes     REPEAT step 2 (Repetition) 4. ELSE     Get out of bed 5. Take a shower (Sequencing) 6. Get dressed (Sequencing) 7. Eat breakfast (Sequencing)

Example 2: Doing Homework

1. Get homework list (Sequencing) 2. WHILE homework is not finished (Repetition)     a. Pick next assignment (Sequencing)     b. IF assignment is math (Selection)         Use calculator     c. ELSE IF assignment is English         Use dictionary     d. Complete assignment (Sequencing) 3. Relax! (Sequencing)

Practice: Design an Algorithm

Problem: Making a Sandwich

Design an algorithm for making a peanut butter and jelly sandwich using the three building blocks.

Consider these questions:

  • What steps must happen in sequence?
  • Where might you need to make decisions (selection)?
  • Is there anything that might need to be repeated?

Think about:

  • Checking if you have all ingredients
  • What to do if bread is stale
  • How much peanut butter to spread
  • What if the jar is too hard to open?

Key Takeaways

2.1.A.1

Algorithms use sequencing, selection, and repetition

2.1.A.2

Algorithms contain decision making and looping

2.1.A.3

Selection is based on true/false decisions

2.1.A.4

Repetition continues until desired outcome

2.1.A.5

Order of building blocks affects the outcome

Next Up

Boolean expressions and conditional statements!

Ready to code with selection and repetition!

1 / ?