Subscribe Contact

Home  »  Chapter 4 : Repetition
Repetition Concepts

Overview

In programming, we often need to repeat a task or set of steps more than once. I could write the same steps in my code, each on a line, over and over. However, besides being redundant, it might work for a couple of repetitions, but imagine if I needed to run the set of steps ten times, a hundred times, a million times--it would be ridiculous to try to repeat my code that many times. Instead of this, we use repetition structures. Let's take a couple of examples.

Concept: Repetition

Often also called looping, repetition is a common programming construct in which we repeat statements either a set number of times or until a particular condition occurs. The most common constructs for repetition are the while loop and the for loop.

Repetition Example 1


Let's prompt the user for a series of test scores and our program will produce the average of the test scores. To calculate an average we add up all of the numbers (scores) in our list to get a total, and then we divide that calculated total by the number of scores we received from the user (in this example, the number of scores).

Here's a description and flowchart of how we might approach this problem:

As depicted in the flowchart, a common approach would be to set a variable that will hold our running total age (we call this an accumulator). Then prompt the user for the first test score. Then in a repetition structure (loop), we add each newly entered score to the accumulator. After adding we prompt the user for the next score. We repeat these steps over and over until the user indicates there are no more scores to enter (we will take a closer look at how the user indicates this on the next pages). When it exits the loop, the accumulator would contain the total score. We could then calculate the average score after the loop by dividing the total score by the number of scores entered by the user.

Repetition Example 2

For our second example, suppose we have a list of the ages of every person in Utah and we want to calculate the average age.

Looking at census data for 2020, the population of Utah was 3,271,616 people. It would be absurd to try to manually calculate the average of that many numbers (ages). So, if we have a list of all the ages (let's say in a file), we could read those numbers into a Python program, add them all up and then divide by 3,271,616. To do this efficiently, we would use a repetition structure.

Here's a description and flowchart of how we might approach this problem:

As depicted in the flowchart, a common approach would be to set a variable that will hold our running total age (we call this an accumulator). Then we read each age from the data source (we'll learn more about how to read data from outside sources later) and in a repetition structure (loop) we add each newly read age to the accumulator. We repeat that step over and over until there are no more pages to read from the data source. In this example, the loop would repeat 3,271,616 times. When it exits the loop, the accumulator would contain the total age. We could then calculate the average age after the loop by dividing the total age by the number of ages.

In the following several pages we will take a look at the while loop and the for loop which are two of the primary repetition structures that we use in programming. We'll also look at several examples of problems that are solved by repetition structures.



«  Previous : Repetition
Next : Repetition : while Loop  »



© 2023 John Gordon
Cascade Street Publishing, LLC