Start learning free

LESSON 110 OF 348 · FOR LOOPS - LOOPING OVER ARRAYS

For Loop Execution Flow in Kotlin: Buggy Code I

Practice Read the full explanation

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

I'm hungry!
I'll eat some pasta
I'll eat some burgers
I'll eat some pizza

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

I'm hungry!
I'll eat some pasta
I'm hungry!
I'll eat some burgers
I'm hungry!
I'll eat some pizza

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

    What the code is supposed to do

    The array foods holds "pasta", "burgers" and "pizza". The program should say that it is hungry once, then name each food:

    I'm hungry!
    I'll eat some pasta
    I'll eat some burgers
    I'll eat some pizza
    

    The starting code prints I'm hungry! before every food instead:

    I'm hungry!
    I'll eat some pasta
    I'm hungry!
    I'll eat some burgers
    I'm hungry!
    I'll eat some pizza
    

    What goes wrong

    This is the starting code:

    val foods = arrayOf("pasta", "burgers", "pizza")
    
    for (food in foods) {
        println("I'm hungry!")
        println("I'll eat some $food")
    }
    

    Everything between the braces of a for loop is the loop body, and the body runs once for each element. There are three foods, so both println calls run three times. The food lines are right, because the message changes with $food. The hunger line has the same text every time and is meant to appear once, so it is in the wrong place.

    The execution flow of the intended program is: print the hunger line, then run the loop, then stop. The hunger line has to come before the loop begins.

    Fixing it step by step

    1. Decide which lines should repeat. Only println("I'll eat some $food") changes with each element, so it stays in the body.
    2. Take println("I'm hungry!") out of the braces. Cut it from the loop body.
    3. Place it above the for line. Output appears in the order the lines execute, and the expected output starts with the hunger line, so it must come before the loop.

    The fixed code in Kotlin

    val foods = arrayOf("pasta", "burgers", "pizza")
    
    println("I'm hungry!")
    
    for (food in foods) {
        println("I'll eat some $food")
    }
    

    The program now runs from top to bottom: one hunger line, then three passes through the loop with one line each. A few points to remember:

    • Position decides how often a line runs. Above the loop, it runs once at the start. Inside, once per element. Below the closing brace, once at the end.
    • The loop variable is available only inside the braces. That is fine here, because $food is used inside the body.
    • The apostrophes need no escaping. The single quote in I'm is an ordinary character inside a double-quoted string.

    Tracing the execution

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

    1. The array is created.
    2. println("I'm hungry!") runs once and prints the first line.
    3. The loop starts with food set to "pasta". The body prints I'll eat some pasta.
    4. The loop moves to "burgers" and the body prints the second food line.
    5. The loop moves to "pizza" and the body prints the third food line.
    6. No elements are left, so the loop ends and the program finishes.

    The buggy version differs only inside the loop: each pass also prints the hunger line first, which is why that line appears three times.

    Common mistakes

    • Moving the line below the loop. I'm hungry! would then print after all the foods, which is the wrong order.
    • Deleting the hunger line. The expected output starts with it, so it can't be dropped.
    • Moving both lines out of the loop. The second line uses $food, which exists only inside the loop, so it wouldn't compile outside it.
    • Judging by indentation alone. The braces decide what is in the loop. Always check where the closing brace is.

    Checking your fix

    Run the fixed code in the editor above. The output has four lines: I'm hungry! once, followed by the pasta, burgers and pizza lines in that order.

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