Chapter 6
Repetition

How do you make a computer do the same thing over and over again? Surely you don’t have to type the code multiple times, right?

According to the structured program theorem, you can use three basic control structures to solve problems with computer programs: sequencing, selection, and repetition. Sequencing is a fancy way of saying that you need to process one step after another in the right order. And selection is making decisions based on conditions. We’ve done both of these throughout the book already. Our early programs did a lot of sequencing, and then we moved into selection when we had our programs start making decisions based on conditions.

But in order to repeat parts of our programs without duplicating code, we use repetition, where we specify that a set of instructions should repeat as long as a condition is true. Think of it as “Keep asking for input while the user wants to enter more values,” or “Do these five steps over and over until you have no records left.”

How we do this repetition depends on the result we want. We may want to repeat a certain number of times, or once for each item in a list of names, or until the user tells us we’re finished.

The programming language you use will influence how you go about solving your problems. For example, in Go, if you wanted to count from 5 down to 1, you’d write code like this:

 
counter := ​5
 
for​ counter <= ​1​ {
 
fmt.Println(counter)
 
counter--;
 
}

You start the counter at 5, print it out, and subtract 1. The code keeps going until the counter is less than or equal to 1.

C-style languages like JavaScript have a for loop that lets you define the counter variable and the incrementation as part of the declaration of the loop:

 
for​(​var​ counter = 5; counter <= 1; counter--) {
 
console.log(counter);
 
}

A Ruby developer might approach this differently; in Ruby, integers are objects that support repetition:

 
5.times ​do​ |counter|
 
# counter is 0-based.
 
puts 5 - counter
 
end

But some languages, like Elixir, don’t have loops and instead rely on recursion:

 
defmodule​ Recursion ​do
 
def​ loop(n) ​when​ n <= 1, ​do​: IO.puts n
 
 
def​ loop(n) ​do
 
IO.puts n
 
loop(n - 1)
 
end
 
end
 
 
loop(5)

Recursion occurs when a function calls itself, either directly or indirectly. In the preceding example, the loop function calls itself, subtracting 1 each time until n is less than 1. Not every language is optimized for recursion though; if you make a function call itself too many times in some languages, it’ll make too many copies of itself and fill up the stack, crashing the program. So once again, the programming language you choose determines the approach you’ll take.

These examples are all counted loops, where you count up or down to a known value. But other times you may need a different reason to stop. You might stop when a certain value is entered:

 
var​ value;
 
var​ keepGoing = true;
 
while​(keepGoing) {
 
value = prompt(​"Enter a number or 0 to stop."​);
 
keepGoing = value !== 0;
 
// more stuff
 
}

Or you might keep going while there are more lines in the file to process or while there are more records from the database to display.

The exercises in this chapter will require you to use repetition to get them to work efficiently. As you read each program’s problem statement, think carefully about whether you’re being asked to do something a specific number of times or an unknown number of times. Then pick the most appropriate approach.

You may find that using flowcharts will help you determine the logic. Remember our program in Chapter 4, Making Decisions where we used a flowchart to help with the logic? We had to prompt for a number greater than 100. We ended the program if we didn’t get the input we wanted. But we could use repetition to keep asking, and we can use a flowchart to represent that logic:

images/repetition/flowchart.png

This flowchart helps clarify that the program contains a repeating process; when the user enters an incorrect value, we prompt them again. From here we can determine the best way to implement that process with our code.

The exercises in this chapter are somewhat simplistic, but they’ll help you get ready for the chapters that follow, which will rely heavily on repetition. Work through these to get the solid grounding you need.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.89.85