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
- Locate the line that repeats too often. Compare the wrong output with the expected one. Only
I'm hungry!appears too many times. - Remove it from the loop body. Delete that line from between the braces, leaving only the
printwith\(food)inside. - 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
forruns first, once. - 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.
foodexists 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
ifto 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 somefollowed 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.
