What the code is supposed to do
The program should greet each student in the students array and then announce the class once, at the end:
Hey, Andy
Hey, Mike
Hey, Mary
Let's start the class!
The starting code announces the class after every greeting:
let students = ["Andy", "Mike", "Mary"]
for student in students {
print("Hey, \(student)")
print("Let's start the class!")
}
Hey, Andy
Let's start the class!
Hey, Mike
Let's start the class!
Hey, Mary
Let's start the class!
What goes wrong
The body of a for loop is everything between its braces, and it runs completely for every element. With three students, the body runs three times. Both print calls are in the body, so the announcement is printed three times, each time right after a greeting.
The greeting is correctly inside the loop, because it must change with each student. The announcement is a different kind of statement: it should happen once, when all the greetings are done. Code that must run once after a loop belongs after the loop's closing brace.
Fixing it step by step
- Identify which statement is out of place. The greeting uses the loop variable, so it must stay. The announcement does not use it.
- Close the loop right after the greeting. Move the closing brace up so the body holds only
print("Hey, \(student)"). - Put the announcement below the brace. The program runs top to bottom: the loop finishes first, then the next line runs, once.
The fixed code in Swift
let students = ["Andy", "Mike", "Mary"]
for student in students {
print("Hey, \(student)")
}
print("Let's start the class!")
Points worth knowing:
- Before or after. This is the mirror image of putting a once-only line at the top. Lines above the loop run before it starts, lines inside run per element, and lines below run after the last element.
- The loop variable disappears at the closing brace.
studentis available only inside the loop. A line below the braces can't use it, and it is not needed there. - The count follows the array. If you add a fourth name to
students, the loop prints a fourth greeting, and the announcement still appears once.
Tracing the fixed program
Following the program line by line shows why the output is right. The array is created. The loop starts with student set to "Andy" and prints the first greeting. The body ends, the loop moves to "Mike" and prints the second, then does the same for "Mary". With no elements left, the flow leaves the loop and reaches the last line, which prints the announcement once. Tracing a program like this, one line at a time, is the quickest way to find where a once-only statement has ended up inside a loop.
Common mistakes
- Moving the announcement above the loop. It prints once, but first, so the order of the output is wrong.
- Relying on indentation. Indenting the announcement less than the greeting does not take it out of the loop. Only the brace does.
- Deleting a brace. A missing closing brace is a compile error, and a brace in the wrong place changes which lines repeat.
- Altering the text. The announcement must read exactly
Let's start the class!, with the apostrophe and the exclamation mark, because the lesson's tests compare the output.
Checking your fix
Run the program in the editor above. The output should have three greetings in array order followed by a single announcement. If the announcement appears between greetings, it is still inside the braces.
