The problem without loops
Programs often repeat the same action. Suppose a student has to write I made a mistake five times. With only println, you write the statement five times:
println("I made a mistake")
println("I made a mistake")
println("I made a mistake")
println("I made a mistake")
println("I made a mistake")
Each line prints the text and moves to a new line, so the output is five identical lines. For five repeats this is ugly but manageable, and copy and paste makes it fast.
Why this doesn't scale
Now the count changes to 100, or to 100,000. Writing that many statements by hand is impractical, and a source file that long is impossible to read. Smaller changes are a problem too:
- Editing the message means changing every copy. Miss one and the output is inconsistent.
- Changing the count means adding or deleting lines and counting them.
- The count may not be known when you write the program. If it depends on the data, such as the number of names in an array, you can't write the right number of statements in advance.
The code is the same each time; only the number of repeats varies. A loop is the tool that expresses this: you write the statement once and tell the program how many times, or for how long, to run it.
What loops give you
Scala has two main loops, and the following lessons cover them:
- the
whileloop repeats a block as long as a condition holds; - the
forloop runs a block once for each element of a collection or for each number in a range.
A preview
You don't need to understand this yet, but here is the same idea with a loop, using three repeats:
for (i <- 1 to 3) println("I made a mistake")
I made a mistake
I made a mistake
I made a mistake
The text appears once in the source. To repeat it 100,000 times, you would change the 3, and the message stays in a single place, so editing it means one change. A while loop reaches the same result by repeating a block as long as a condition holds, and the next lessons build both loops step by step.
The exercise
This lesson has you feel the problem first. Print I promise to learn coding. five times using only println statements:
println("I promise to learn coding.")
println("I promise to learn coding.")
println("I promise to learn coding.")
println("I promise to learn coding.")
println("I promise to learn coding.")
The text must match exactly, including the capital I and the full stop at the end. Output:
I promise to learn coding.
I promise to learn coding.
I promise to learn coding.
I promise to learn coding.
I promise to learn coding.
The statements run one after another, from the first line down, so five statements give five lines. Notice how much of the code is the same line; that repeated part is what the next lessons remove.
Key takeaways
- Copying a statement to repeat it is slow, error-prone and impossible when the count is not known in advance.
- A loop lets you write the repeated code once.
- Scala's loops are
while, which repeats while a condition is true, andfor, which walks through a collection or a range.
