2.1 Algorithms with Selection and Repetition
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class periodNot autograded
Files
2.1 Algorithms with Selection and Repetition
This lesson has no coding exercises.
Every algorithm consists of a sequence of steps. Up until now, we have been writing code one line at a time in sequence 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. In this unit, we will learn to branch the code into different paths or repeat a block of code.
The building blocks of algorithms are sequencing, selection, and repetition. Algorithms can contain selection, through decision making, and repetition, via looping. In fact, it’s been proven that all algorithms for problems that can be solved on a computer can be constructed by using just these three control structures: sequence, selection, and repetition as seen below.

Sequence, Selection, and Repetition
Selection
Selection is the process of making a choice based on a true or false decision. In programming, selection (also called branching) occurs when a choice of how the execution of an algorithm will proceed is based on a true or false decision. An algorithm may take different paths based on certain conditions, often using true/false logic.
Check your understanding: Click on all of the selection (decision or branching) phrases in the following algorithm.
Morning routine: 1. Wake up. 2. Snooze for 5 more minutes. 3. Check your phone and the weather 4. If there is a text from your friend, answer it. 5. Brush teeth and shower. 6. If it’s cold, wear a sweater. 7. Check if you have homework due. If so, pack it in your bag. 8. Put on your sunglasses if it's sunny. 9. Leave for school or work.Answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/topic-2-1-algorithms.html#clickSelection
Repetition
Repetition is when a process repeats itself until a desired outcome is reached. In programming, repetition is achieved through loops. A loop allows an algorithm to repeat certain actions until a specified condition is met. Another term commonly used for repetition is iteration.
Check your understanding: Click on all of the repetition phrases in the following algorithm.
Morning routine 2: 1. Wake up. 2. Snooze for 5 more minutes. Keep waking up and snoozing for the next 15 minutes 3. If there is a text from your friend, answer it. Do this for all of your texts. 4. Brush teeth and shower. 5. If it’s cold, wear a sweater. 6. Check if you have homework due. If so, pack it in your bag. 7. Repeat packing items until your bag is ready. 8. Leave for school or work.Answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/topic-2-1-algorithms.html#clickRepetition
Algorithms with Pseudocode and 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 like below. Selection is usually represented as a triangle in a flowchart, and arrows are used to show repetition. Both pseudocode and flowcharts are tools that help you plan your algorithm before writing code.

Flowchart for Selection branching the code into two paths
The order in which sequencing, selection, and repetition are used contributes to the outcome of the algorithm. In the following pseudocode, put the steps of the algorithm in order and then decide what the outcome will be based on the order of the steps and the conditions.
Check your understanding: Put the following pseudocode algorithm steps for buying a birthday gift for your friend in order. Make sure some of the steps are indented to be inside the repetition and that you buy the more expensive gifts first. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/topic-2-1-algorithms.html#algorithm-gift
Check your understanding: Given the pseudocode for buying a gift above, assume you have $16 to spend. What will be the outcome of the algorithm? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/topic-2-1-algorithms.html#trace-algorithm-gift1
Check your understanding: Given the pseudocode for buying a gift above, assume you have $2 to spend. What will be the outcome of the algorithm? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/topic-2-1-algorithms.html#trace-algorithm-gift2
Coding Challenge: Algorithms
In this group activity, you will work together to create an algorithm for a common problem: choosing a snack! Create pseudocode for this problem. Make sure you include selection and repetition in your algorithm. For example, if there is no chocolate, you may want to keep searching for another snack. If you’re thirsty, you may want to consider the drinks in the fridge. You may want to consider every item in the fridge or your leftover halloween candy stash before deciding on the perfect snack. Be creative!
Write your pseudocode for choosing a snack here. Include selection and repetition in your algorithm. Be creative!
Discuss this with your class, or answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/topic-2-1-algorithms.html#challenge-algorithm-snack
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.
For teachers
2.1 Algorithms with Selection and Repetition
Students read CSAwesome2's “Algorithms with Selection and Repetition” in CS Room and work through its questions in class or on CS Awesome.
- Time: 1 class period
- Tests: none
Goals
- (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.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft.
- Publish it.
- The original page, with its click-the-answer, free-response, mixed-up code and multiple-choice questions, is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/topic-2-1-algorithms.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. Questions that need CS Awesome's interactive widgets link back to it.
Exercises
None.