Start learning free

LESSON 113 OF 347 · WHILE LOOPS & FOR LOOPS

Why Loops in C#: Repeating Code Without Copy and Paste

Lesson Read the full explanation

Loops are probably the most powerful tool a programming language can offer. You can run the same code multiple times by using a loop.

To really understand their value, let’s take some time to see how frustrating our life would be if a repeated task required us to type out the same code every single time.

Imagine this: A student made a mistake and the teacher asked them to print I made a mistake 5 times to the console.

If we only use Console.WriteLine(), our program might look 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");

That’s ugly, but still manageable. After all, we’re writing only 5 lines of code and most probably copying and pasting a few times.

Now imagine if we come back to this program and we wanted to print the message for 10, 100, 100000 times? It would take an extremely long time and by the end, we could still end up with inconsistencies and mistakes.

We’ll learn how loops come to our rescue in the next lesson. But for now, let’s gain an appreciation for loops.


Assignment
Follow the Coding Tutorial to see how frustrating our life is without loops.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · WHILE LOOPS & FOR LOOPS

    Why Loops in C#, explained

    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 while loop, which repeats a block as long as a condition is true;
    • the foreach loop, which visits every element of an array or list;
    • the for loop, 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.

    1. Write one statement that prints the sentence: Console.WriteLine("I promise to learn coding.");.
    2. 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.Write instead of Console.WriteLine. Write doesn'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, for and foreach loops, which the next lessons introduce.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free