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
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
Multi-way Selection
Repetition: Repeating Actions
What is Repetition?
Repetition is when a process repeats itself until a desired outcome is reached.
Real-World Example: Making Coffee
↑ (Repeat)
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
For Loops
Enhanced For Loops (For Arrays/Collections)
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
Algorithms in Everyday Life
Example 1: Getting Ready for School
Example 2: Doing Homework
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!