Section2.1Algorithms with Selection and Repetition
45 minutes
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.
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.
Selections are made based on a true or false decision. Look for the word "if" to identify selections.
Morning routine:
1. Wake up.2. Snooze for 5 more minutes.3. Check your phone and the weather4. 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.
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.
Repetition is when a process repeats itself until a desired outcome is reached. Look for the word "repeat" or "all" or "keep" to identify repetition.
Morning routine 2:
1. Wake up.2. Snooze for 5 more minutes. Keep waking up and snoozing for the next 15 minutes3. 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.
Subsection2.1.3Algorithms 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.
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.
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. Click on Check Me to see if you are right.
Initialize total amount of money to a whole number.
---
Repeat while total is greater than or equal to $1:
---
If total is greater than or equal to $25, buy a gift card and subtract 25 from total.
---
If total is greater than or equal to $10, buy a small cake and subtract 10 from total.
---
If total is greater than or equal to $5, buy some candy and subtract 5 from total.
---
If total is greater than or equal to $1, buy a birthday card and subtract 1 from total.
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!