Start learning free

LESSON 115 OF 415 · FOR LOOPS - LOOPING OVER ARRAYS

For Loop Execution Flow in Swift: Buggy Code I

Practice Read the full explanation

Inside the code editor we've tried to write a program that should print:

I'm hungry!
I'll eat some pasta
I'll eat some burgers
I'll eat some pizza

but it seems like we made some mistakes because when we run our code, it produces this output:

I'm hungry!
I'll eat some pasta
I'm hungry!
I'll eat some burgers
I'm hungry!
I'll eat some pizza

Assignment:

Your task is to fix our code such that it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · FOR LOOPS - LOOPING OVER ARRAYS

    Buggy Code For Loops Execution Flow 1 in Swift, explained

    What the code is supposed to do

    The program should print one greeting and then a line for each food in the foods array:

    I'm hungry!
    I'll eat some pasta
    I'll eat some burgers
    I'll eat some pizza
    

    The code you start with prints I'm hungry! before every food instead:

    let foods = ["pasta", "burgers", "pizza"]
    
    for food in foods {
        print("I'm hungry!")
        print("I'll eat some \(food)")
    }
    
    I'm hungry!
    I'll eat some pasta
    I'm hungry!
    I'll eat some burgers
    I'm hungry!
    I'll eat some pizza
    

    What goes wrong

    Everything between the loop's braces runs once per element. The array has three foods, so the body runs three times, and both print calls run each time. The line that builds the food message is right: it uses \(food), so it changes on every pass. The line print("I'm hungry!") is in the wrong place. It has no reason to repeat, but it sits inside the braces, so it does.

    The rule to apply is simple: a statement that should run once per element goes inside the loop, and a statement that should run once goes outside. Here, the greeting should run once, and it must come first, so it belongs before the loop.

    Fixing it step by step

    1. Locate the line that repeats too often. Compare the wrong output with the expected one. Only I'm hungry! appears too many times.
    2. Remove it from the loop body. Delete that line from between the braces, leaving only the print with \(food) inside.
    3. Put it before the loop. The greeting must be the first line of output, and a program runs from top to bottom. A call placed above for runs first, once.
    4. Leave the loop alone. The header and the food line are already correct.

    The fixed code in Swift

    let foods = ["pasta", "burgers", "pizza"]
    
    print("I'm hungry!")
    
    for food in foods {
        print("I'll eat some \(food)")
    }
    

    Some details to remember:

    • Position matters. The same line placed after the loop would run once, but it would print last, after the three foods. Before the loop it prints first, which is what the output asks for.
    • Indentation is for people. Swift reads the braces, not the spaces. Indenting print("I'm hungry!") or not does not change which lines are in the loop. The four-space indent inside the braces is a convention that makes the structure visible.
    • The loop variable is local. food exists only inside the braces. The greeting before the loop cannot use it, and it does not need to.

    Common mistakes

    • Moving the greeting after the loop. The program runs but prints the greeting at the end, so the output order is wrong.
    • Deleting the greeting. Without it the output has only the three food lines.
    • Printing it once per element in a different way. Adding a counter or an if to print the greeting only on the first pass works but is more complicated than moving a single line.
    • Changing the food line. The text I'll eat some followed by the food name is already right. Extra spaces or a changed word would fail the lesson's tests, which compare the output exactly.

    Checking your fix

    Run the program in the editor above. A correct fix prints four lines: the greeting first, then pasta, burgers and pizza in array order. If the greeting still appears more than once, a copy is still inside the braces.

    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free