Skip to main content

Section 13.3 Decisions and Loops

Almost always, an algorithm needs to make a decision. We saw that in the 8 am attendance problem solution. The algorithm helped choose clothing based on the variable forecast. As mentioned earlier, computers are fast but really dumb. We can’t just ask a computer what we should wear. We need to be specific in the steps it needs to follow to determine what we wear. Whenever we need to have a computer make a decision, we use an if statement. (e.g. if forecast >= 60)
Another thing that computers are really good at is doing boring, repetitive tasks over and over. For example, let’s think about a computer program that assigns people to a military draft. Let’s say that we need to draft 20,000 men into the armed services. We can write a computer program that randomly assigns all eligible men a draft number, then randomly chooses 20,000 numbers. If a human had to do this, it would take a very long time, but a computer can get this done very quickly! Whenever we need a computer to do a task over and over again (aka: loop) we use a for statement.
Decisions- if Statements
if statements are actually very simple and elegant ways to get computers to make decisions. They work by evaluating some conditional statements. If the statement evaluates to true, you do what follows. Optionally, you can include an else to specify what to do if the if statement evaluates to false.
Generically, if statements look like this:
If conditions then do
sequence 1
Else do
sequence 2
For example, consider line 4 in the pseudocode in figure 13.2.4:
If forecast >= 70, continue, else go to line 7.
The decision is made based on the conditional statement. If the forecast temperature is greater than or equal to 70 degrees you continue, else you go to line 7. That is the decision! Hopefully, you know and understand how this works, and if you do, you are already thinking algorithmically!
Consider that a student designs an algorithm that informs them how much they should be studying each school day for an exam. The algorithm makes a decision about how much the student should study based on how far away the exam is. The student β€œruns” the algorithm once a day in the morning. Consider that the exam is 9 days away.

Checkpoint 13.3.1. Study Time.

How much time does the student spend studying according to this algorithm?
  • 30 minutes
  • Incorrect. Carefully follow each step of the algorithm and add up the total study time.
  • 15 minutes
  • Incorrect. The student spends more time studying than this.
  • 1 hour
  • Correct. Following the algorithm step-by-step shows that the student spends a total of 1 hour studying.
  • 2 hours
  • Incorrect. The total study time is less than 2 hours.
Loopd- for Statements
for statements allow us to write down repetitive actions algorithmically. Consider the generic for statement to get an idea of how it works:
for number of iterations
sequence repeats a number of iterations
The idea of a for statement is that you already have an idea of how many iterations need to be performed, then you algorithmically define the sequence that is to be performed beneath it. Let’s return to the military drafting example to illustrate this. Consider the algorithm below that assigns all eligible men a draft number, then randomly chooses 20,000 numbers.
Figure 13.3.2. for statement example algorithm
Looking at figure 13.3.2 above, hopefully, you can see why it is called a loop. Starting at the top, we first assign all eligible men a draft number. Let’s say there are 150,000 eligible men, so the next line tells us to set the variable number_eligible to 150,000. On line three we have our for statement and enter the loop. The statement is telling us to start drafting at 1 and continue until 20,000. On line 4 we choose a random number, then on line 5, we draft the person with that number. Now the loop comes into play. Because we are in a for loop, we automatically increment draft by one and continue repeating lines 4 and 5 19,999 more times.
I understand that for loops can be tricky. Take some time to think about this section and if you are still confused supplement your reading with the Wikipedia page on for loops. Don’t skip your brain workouts!
You have attempted of activities on this page.