Start learning free

LESSON 112 OF 348 · FOR LOOPS - LOOPING OVER ARRAYS

For Loop Execution Flow in Kotlin: Buggy Code III

Practice Read the full explanation

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

What a nice day!
Hey, Andy
How are you?
Hey, Mike
How are you?
Hey, Mary
How are you?
Let's start the class!

but it seems like we made some mistakes because when we run our code, it produces a different output.


Assignment:

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

STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · FOR LOOPS - LOOPING OVER ARRAYS

    Buggy Code For Loops Execution Flow 3 in Kotlin, explained

    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:

    1. Once at the start: What a nice day!
    2. Once per student: Hey, $student followed by How are you?
    3. 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, $student is after the loop too. The loop variable student exists only between the braces, so outside them the compiler reports unresolved reference 'student'. Because of that single error, none of the program runs.

    Fixing it step by step

    1. Write the opening line first. println("What a nice day!") goes above the loop, where it runs once.
    2. Put both per-student lines in the loop body, in the order the output shows: the greeting with $student first, then How are you?. Now student is used where it exists, and the error disappears.
    3. 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 $student had 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 where student is in scope.

    Tracing the execution

    Following the fixed program line by line shows why it works:

    1. The array is created, and What a nice day! prints once.
    2. The loop starts with student set to "Andy". The body prints Hey, Andy and then How are you?.
    3. The same two lines print for "Mike" and then for "Mary".
    4. 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, $student into 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!.

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