The problem loops solve
Programs often need to do the same thing many times: print a message, process every item in a list, or count to a number. With only the tools you have so far, repetition means writing the same statement again and again.
Suppose a teacher asks a student to print I made a mistake five times. With Console.WriteLine alone, the program looks like this:
Console.WriteLine("I made a mistake");
Console.WriteLine("I made a mistake");
Console.WriteLine("I made a mistake");
Console.WriteLine("I made a mistake");
Console.WriteLine("I made a mistake");
It runs, and for five lines it is tolerable. The trouble starts when the requirements change.
What goes wrong as the number grows
- The size of the program follows the count. Printing the message 100 times takes 100 lines. Printing it 100,000 times takes 100,000 lines, which nobody wants to write or read.
- Changing the repeated text means changing every copy. If the message should read
I made a mistake, sorry, you have to edit every line. Miss one and the output is inconsistent. - The count is hidden. To know how many times something repeats, you have to count the lines by eye. A line pasted twice by accident, or left out, changes the result silently.
- The count can't depend on data. If the number of repeats comes from the user, or from the length of an array, you can't know how many lines to write in advance. Copy and paste can't handle that case at all.
All four problems share one cause: the program says what to do many times, when it should say it once and describe how many times to do it. That is exactly what a loop does. A loop takes a block of code and runs it repeatedly, while a condition holds or once for each item of a collection.
What loops give you
Later lessons show how to write each kind of loop in C#. In short, you'll get:
- the
whileloop, which repeats a block as long as a condition is true; - the
foreachloop, which visits every element of an array or list; - the
forloop, which counts with an index.
With a loop, repeating a message 5 times or 100,000 times differs by a single number, and the message appears in only one place in the code. Changing the text means editing one line.
The exercise, step by step
This lesson asks you to feel the problem rather than solve it. The task is to print I promise to learn coding. five times, using only Console.WriteLine.
- Write one statement that prints the sentence:
Console.WriteLine("I promise to learn coding.");. - Repeat it four more times, so there are five identical lines, one after another.
Console.WriteLine("I promise to learn coding.");
Console.WriteLine("I promise to learn coding.");
Console.WriteLine("I promise to learn coding.");
Console.WriteLine("I promise to learn coding.");
Console.WriteLine("I promise to learn coding.");
Each statement runs in order from top to bottom, so the sentence appears on five lines. WriteLine adds a line break after the text, which is why they don't run together.
Common mistakes
- Typing the text slightly differently. The tests compare the output exactly, so a missing period, an extra space or different capitalisation in any one of the five lines makes the output wrong.
- Using
Console.Writeinstead ofConsole.WriteLine.Writedoesn't add a line break, so the five sentences would end up on a single line. - Too many or too few lines. It's easy to lose count when copying and pasting. Count the lines. This is itself a reason loops are needed.
Key takeaways
- Repeating a statement by copying it makes a program longer, harder to change and easy to get wrong.
- A loop states the repeated code once and says how often it runs.
- C# has
while,forandforeachloops, which the next lessons introduce.