What the code is supposed to do
The program should open with one line, greet each student with a follow-up question, and close with one line:
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 at once. It doesn't even compile:
val students = arrayOf("Andy", "Mike", "Mary")
println("Let's start the class!")
for (student in students) {
println("What a nice day!")
}
println("How are you?")
println("Hey, $student")
What goes wrong
Sort the expected output into three parts:
- Once at the start:
What a nice day! - Once per student:
Hey, $studentfollowed byHow are you? - Once at the end:
Let's start the class!
Now compare with the starting code. Each line is in the wrong part:
Let's start the class!comes first, but it must come last.What a nice day!is inside the loop, so it would print three times, but it must print once at the start.How are you?is after the loop, so it prints once, but it must follow each greeting.Hey, $studentis after the loop too. The loop variablestudentexists only between the braces, so outside them the compiler reportsunresolved reference 'student'. Because of that single error, none of the program runs.
Fixing it step by step
- Write the opening line first.
println("What a nice day!")goes above the loop, where it runs once. - Put both per-student lines in the loop body, in the order the output shows: the greeting with
$studentfirst, thenHow are you?. Nowstudentis used where it exists, and the error disappears. - Move the closing line to the end.
println("Let's start the class!")goes after the closing brace, where it runs once, after the last student.
The fixed code in Kotlin
val students = arrayOf("Andy", "Mike", "Mary")
println("What a nice day!")
for (student in students) {
println("Hey, $student")
println("How are you?")
}
println("Let's start the class!")
Reading it from top to bottom gives the execution flow: one line, then three passes of two lines each, then one line. A few points to remember:
- Scope. A loop variable is visible only inside the braces of its loop. This is why
$studenthad to move into the body. - Several lines per pass. The body can hold any number of statements. They run in order on every pass, so each student gets the greeting and then the question.
- A string template can only use names that exist at that point. The text
"Hey, $student"compiles only wherestudentis in scope.
Tracing the execution
Following the fixed program line by line shows why it works:
- The array is created, and
What a nice day!prints once. - The loop starts with
studentset to"Andy". The body printsHey, Andyand thenHow are you?. - The same two lines print for
"Mike"and then for"Mary". - No elements are left, so the loop ends and
Let's start the class!prints once.
That is 1 + 3 x 2 + 1 = 8 lines, which matches the expected output. The same counting works as a check on any fix: if your output has a different number of lines, one of the statements is inside or outside the loop when it should be the other.
Common mistakes
- Fixing only the compile error. Moving just
Hey, $studentinto the loop makes the program compile, but the output order is still wrong. - Swapping the two lines in the body.
How are you?before the greeting prints the question first for every student. - Leaving the opening line inside the loop. It would print three times.
Checking your fix
Run the fixed code in the editor above. The output should have eight lines: the opening line, then a greeting and a question for each of Andy, Mike and Mary, and finally Let's start the class!.