π2.1: Selection & Repetition Algorithms
Table of Contents
π This page is a condensed version of CSAwesome Topic 2.1
Algorithms with Control Structures
Every algorithm consists of a sequence of steps. Up until now, we have been writing code instructions one line at a time, which then gets executed by the computer in that sequential order by default. However, we can create more complex algorithms that do not follow the default sequential order.
π§± Control structures are the building blocks of all algorithms: sequencing, selection/branching, and repetition/iteration.
In fact, itβs been proven that all problems that can be solved on a computer can be constructed by using just these three control structures!
Algorithms can implement control structures that:
- π Branch the code execution into different paths using SELECTION
Conditional decision-making (
if
,else if
,else
) - π Repeat a block of code using ITERATION
Loops (
while
,for
,for each
)
Selection (Branching)
- Selection
- When a choice of how a process will proceed is based on a true or false decision.
π In programming, an algorithm may take different paths (run different lines of code) depending on certain conditions.
π Identify all the selection (decision/branching) phrases in the following algorithm:
- Wake up.
- Check your phone and the weather.
- If there is a text from your bestie, answer it.
- Brush teeth and shower.
- If itβs below 50 degrees, wear a sweater over your polo.
- Otherwise, pack the sweater in your bag just in case.
- Check if you have homework due. If so, pack it in your bag.
- Put on your sunglasses if itβs sunny.
- Leave for school.
HINT: Selections are made based on a true or false decision. Look out for the word
"if"
, and sometimes"otherwise"
, to identify selections.
Repetition (Iteration)
- Repetition
- When a process repeats itself, or iterates, until a specified condition is met.
π In programming, repetition is achieved through loops, which are code blocks that execute again and again until a desired outcome is reached.
π Identify all the repetition (iteration) phrases in the following algorithm:
- Wake up.
- Snooze for 5 more minutes. Repeat waking up and snoozing for the next 15 minutes.
- If there is a text from your friend, answer it. Do this for all of your texts.
- Brush teeth and shower.
- Check if you have homework due. If so, pack it in your bag.
- Keep packing items until your bag is ready.
- Leave for school.
HINT: Repetition is when a process repeats itself until a desired outcome is reached. Look for the words
"repeat"
,"all"
, or"keep"
to identify repetition.
Planning with Pseudocode & Flowcharts
For complex problems, it is important to plan your solution before writing code.
- Pseudocode is a simplified, informal way of describing the steps in an algorithm in a human language like English but following the sequence, selection, and repetition structure of a programming language.
- Flowcharts are diagrams that represent the steps in an algorithm. Selection is usually represented as a triangle in a flowchart, and arrows are used to show repetition.
The order in which sequencing, selection, and repetition are used contributes to the outcome of the algorithm.
To understand why ORDER MATTERS, consider this pseudocode algorithm for buying a birthday gift:
- Initialize
total
amount of money to spend. - Repeat while still have money in
total
:- If
total
more than $25, buy a gift card and subtract 25 fromtotal
. - If
total
more than $10, buy a small cake and subtract 10 fromtotal
. - If
total
more than $5, buy some candy and subtract 5 fromtotal
. - If
total
more than $1, buy a card and subtract 1 fromtotal
. - Otherwise, give them the change.
- If
π¬ Decide what the outcome will be if you have $16
to spend, based on the order of the steps and the conditions. What if you had $22
?
Afterwards, switch the order of some of the steps and discuss how the outcomes might change.
Group Challenge: Algorithm Design
π« In small groups, design an algorithm for a common problem: choosing a snack. Write pseudocode that includes both selection and repetition, and then draw a flowchart.
Make it customized to your own preferences and experiences!
Examples:
- If there are Oreos today, grab one immediately and head back to class.
- If there are no good options in the Upper School lounge, go check the Middle School snack shelves.
- You may want to consider every item in both snack areas, or search all the leftover candy in your locker, before deciding.
- If there is something salty, like Cheez-its or Goldfish, grab an extra one for Mrs. Walter.
Summary
- (AP 2.1.A.1) The building blocks of algorithms include sequencing, selection, and repetition.
- (AP 2.1.A.2) Algorithms can contain selection, through decision making, and repetition, via looping.
- (AP 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.
- (AP 2.1.A.4) Repetition is when a process repeats itself until a desired outcome is reached.
- (AP 2.1.A.5) The order in which sequencing, selection, and repetition are used contributes to the outcome of the algorithm.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.