Start learning free

LESSON 119 OF 415 · WHILE LOOPS & FOR LOOPS

Why Loops? Repeating Code in Swift

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 promise to learn coding. 5 times to the console.

If we only use print(), our program might look like this:

print("I promise to learn coding.")
print("I promise to learn coding.")
print("I promise to learn coding.")
print("I promise to learn coding.")
print("I promise to learn coding.")

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 2
    Ctrl + Enter to check

    SWIFT · WHILE LOOPS & FOR LOOPS

    Why Loops in Swift, explained

    The problem loops solve

    Programs often repeat work. Suppose a teacher asks a student to print a sentence five times. Without loops, the only tool is the print function, called once per line.

    print("I promise to learn coding.")
    print("I promise to learn coding.")
    print("I promise to learn coding.")
    print("I promise to learn coding.")
    print("I promise to learn coding.")
    

    Each call prints the text and ends the line, so the output is five identical lines. The program works, and for five lines the cost is small.

    Why it does not scale

    Now change the request to 10 lines, then 100, then 100,000. Writing that many lines by hand is slow, and copying and pasting invites mistakes: one line may be missed, one may be pasted twice, and a typo in one copy leaves the output inconsistent.

    A second problem is change. If the sentence has to change to I promise to practise every day., every one of those lines has to be edited. With 100,000 copies, that is a large job, and any copy left behind gives a wrong result.

    What you want to say is: print this sentence, and do it five times. The sentence should appear in the program once, and the number of repetitions should be a single value you can change. A loop expresses exactly that.

    What a loop gives you

    • Less code. The repeated statement is written once, however many times it runs.
    • One place to change. Edit the statement or the count in one spot.
    • Counts that are not fixed. A loop can repeat once per element of an array, whose length may not be known when you write the program.

    Swift has two main loops. A while loop repeats as long as a condition is true. A for-in loop repeats once for each element of a sequence, such as an array or a range like 1...5. The next lessons teach them, starting with while.

    The exercise

    The assignment is to feel the cost of the manual approach. Using only print, print "I promise to learn coding." five times. The text must match exactly, including the capital I and the full stop, because the lesson's tests compare the output.

    1. Write one call: print("I promise to learn coding.").
    2. Repeat it on four more lines, five calls in total.
    3. Count the lines. Four calls give four lines, six give six. Only five passes.

    The program is the five-line block shown above. Notice how repetitive it is, and keep that feeling in mind for the next lesson.

    Common mistakes

    • Printing the wrong number of lines. Count the calls before running.
    • Changing the sentence in one line. A lowercase letter or a missing full stop in one copy fails the tests.
    • Printing everything in one call. One print call with the sentence five times in it gives one line, not five.

    Key takeaways

    • Repeating a statement by hand is slow, error-prone and hard to change.
    • Loops run a block of code many times while it is written once.
    • Swift's two loops are while and for-in.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free