Time estimate: 45 min.

2.1. Algorithms with Selection and Repetition

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.

../_images/algorithms.png

Figure 1: Sequence, Selection, and Repetition

2.1.1. 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.

exercise Check Your Understanding

2.1.2. 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.

exercise Check Your Understanding

2.1.3. 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.

../_images/Condition-two.png

Figure 2: 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.

exercise 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. Click on Check Me to see if you are right.

2.1.4. groupwork 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!

2-1-6: Write your pseudocode for choosing a snack here. Include selection and repetition in your algorithm. Be creative!

2.1.5. 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.

You have attempted of activities on this page