Start learning free

LESSON 111 OF 348 · FOR LOOPS - LOOPING OVER ARRAYS

For Loop Execution Flow in Kotlin: Buggy Code II

Practice Read the full explanation

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

Hey, Andy
Hey, Mike
Hey, Mary
Let's start the class!

but it seems like we made some mistakes because when we run our code, it produces this output:

Hey, Andy
Let's start the class!
Hey, Mike
Let's start the class!
Hey, Mary
Let's start the class!

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 2 in Kotlin, explained

    What the code is supposed to do

    The array students holds "Andy", "Mike" and "Mary". The program should greet each student, and then announce the class once, at the end:

    Hey, Andy
    Hey, Mike
    Hey, Mary
    Let's start the class!
    

    The starting code prints the announcement after every greeting:

    Hey, Andy
    Let's start the class!
    Hey, Mike
    Let's start the class!
    Hey, Mary
    Let's start the class!
    

    What goes wrong

    This is the starting code:

    val students = arrayOf("Andy", "Mike", "Mary")
    
    for (student in students) {
        println("Hey, $student")
        println("Let's start the class!")
    }
    

    Both println calls are between the braces, so both belong to the loop body, and the body runs once for each of the three students. The greeting is meant to repeat. The announcement is not: it has the same text for everyone and belongs after the last greeting.

    In the intended flow, the loop runs first and finishes. Only then does the program move to the line after the closing brace.

    Fixing it step by step

    1. Keep the greeting inside the loop. It uses $student, so it must run once per name.
    2. Move the announcement out of the braces. Cut println("Let's start the class!") from the body.
    3. Paste it after the closing brace. A line below the loop runs once, after the last pass, which matches the expected output.

    The fixed code in Kotlin

    val students = arrayOf("Andy", "Mike", "Mary")
    
    for (student in students) {
        println("Hey, $student")
    }
    
    println("Let's start the class!")
    

    The loop prints three greetings, then execution continues below the closing brace and prints the announcement a single time. A few points to remember:

    • The closing brace ends the loop. Anything written after it is outside the repetition, whatever the indentation looks like.
    • The blank line is optional. It separates the loop from the final line for readers, and the compiler ignores it.
    • Order of output follows order of execution. The announcement comes last because it is the last thing the program does.

    Tracing the execution

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

    1. The array is created.
    2. The loop starts with student set to "Andy", and the body prints Hey, Andy.
    3. The loop moves to "Mike" and prints Hey, Mike.
    4. The loop moves to "Mary" and prints Hey, Mary.
    5. No elements are left, so the loop ends. Execution continues with the next line after the closing brace, and the announcement prints once.

    In the buggy version the body has two statements, so each of the three passes prints two lines. That gives six lines instead of four.

    A useful habit is to count lines before you run the code. The loop body has one statement and the array has three elements, so the loop contributes three lines. One more line comes after it, for four in total. If the count you expect differs from what the program prints, a line is in the wrong place.

    Common mistakes

    • Moving the announcement above the loop. It would print once, but before the greetings instead of after them.
    • Moving the greeting out of the loop. $student exists only inside the loop, so a greeting below it doesn't compile.
    • Adding an extra closing brace or leaving one out. The compiler reports an error when the braces don't match, or the loop swallows more lines than you meant.

    Checking your fix

    Run the fixed code in the editor above. You should see three Hey, lines for Andy, Mike and Mary, followed by Let's start the class! once.

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