What the code is supposed to do
The program should print an opening line once, then two lines for each student, then a closing line once:
What a nice day!
Hey, Andy
How are you?
Hey, Mike
How are you?
Hey, Mary
How are you?
Let's start the class!
The starting code has several mistakes and doesn't compile:
let students = ["Andy", "Mike", "Mary"]
print("Let's start the class!")
for student in students {
print("What a nice day!")
}
print("How are you?")
print("Hey, \(student)")
The compiler reports cannot find 'student' in scope on the last line. That is only the first problem. Even without it, the lines are in the wrong places.
What goes wrong
Three separate things are wrong, and each comes from the same rule: where a line sits decides how often it runs.
- The closing line is first.
Let's start the class!comes before everything, but the expected output ends with it. A program runs from top to bottom, so it has to be the last line. - The opening line is inside the loop.
What a nice day!would print three times. It should print once, before the loop. - The per-student lines are outside the loop.
How are you?and the greeting come after the closing brace. They would run once, and the greeting usesstudent, a variable that exists only inside the braces. That is what the compiler rejects.
Fixing it step by step
- Decide what runs once at the start.
What a nice day!goes above the loop. - Decide what runs for each student. The greeting and
How are you?go inside the braces, in the order of the expected output: the greeting first, then the question. - Decide what runs once at the end.
Let's start the class!goes below the closing brace.
The fixed code in Swift
let students = ["Andy", "Mike", "Mary"]
print("What a nice day!")
for student in students {
print("Hey, \(student)")
print("How are you?")
}
print("Let's start the class!")
Here is how the program flows with this layout. First the opening line prints. Then the loop runs three times, and each pass prints a greeting with the current name followed by the question. When the students are used up, the loop ends and the last line prints.
- Scope. A loop variable is visible only between the braces of its loop. Using
studentafter the closing brace is an error because the name does not exist there. Moving the line into the loop fixes it. - Order inside the body. Statements in the body run in the order they are written, on every pass. Swapping the two
printcalls would put the question before each greeting. - Warnings. In the starting code the compiler also warns that the loop variable is never used, because the only line that mentions it is outside the loop. The fix removes that warning.
Common mistakes
- Fixing only the compile error. Moving the greeting inside the loop makes the code compile, but the other lines still print in the wrong order and the wrong number of times.
- Reading the expected output by blocks. Group the lines first: one at the start, two repeating, one at the end. That grouping gives the structure of the program.
- Swapping the repeating lines. The expected output shows the greeting before
How are you?each time. - Rewriting the array. The names must stay
Andy,Mike,Maryin that order.
Checking your fix
Run the program in the editor above. A correct fix compiles and prints eight lines, matching the expected output exactly.
