What the lesson covers
A for loop repeats a block of code once for every element of a collection. Kotlin decides which lines belong to the loop with curly braces. Everything between { and } is the loop body and runs once per element. Everything after the closing brace runs once, when the loop is over.
The loop syntax
To visit every element of an array, write the loop variable, the keyword in and the collection:
val languages = arrayOf("Python", "Java", "JavaScript")
for (language in languages) {
println(language)
}
On each pass, language holds the next element, starting with the first. You don't declare it with val or var and you don't give it a type: Kotlin knows each element of this array is a String. You also don't need an index or a counter. The loop stops by itself after the last element.
Which lines are part of the loop
Compare these two programs. In the first, both println calls are inside the braces:
val languages = arrayOf("Python", "Java", "JavaScript")
for (language in languages) {
println("I love $language")
println("I want to get better at it")
}
Both lines run for every language, so the output has six lines:
I love Python
I want to get better at it
I love Java
I want to get better at it
I love JavaScript
I want to get better at it
In the second program, the last println is moved below the closing brace:
val languages = arrayOf("Python", "Java", "JavaScript")
for (language in languages) {
println("I love $language")
}
println("I want to get better at them")
Now only the first line repeats. The second runs once, after the loop has finished:
I love Python
I love Java
I love JavaScript
I want to get better at them
Execution flow
A program starts at its first line and runs one line at a time, from top to bottom. This order is called the execution flow. A loop changes it in one way only: when execution reaches the loop, it runs the body once for each element, then moves on to the line after the closing brace.
val fruits = arrayOf("banana", "apple", "kiwi")
println("I'm hungry!")
for (fruit in fruits) {
println("I'm gonna eat:")
println(fruit)
}
println("I love my life!")
Read it in order. The first println prints once. The loop then runs three times, printing two lines each time. The last println prints once at the end:
I'm hungry!
I'm gonna eat:
banana
I'm gonna eat:
apple
I'm gonna eat:
kiwi
I love my life!
When you are unsure what a program prints, look for each line's position relative to the braces. Before the loop means once at the start, inside means once per element, after means once at the end.
The exercise
The editor holds this code, and the program has to print the heading, then each language on its own line, then Let's code!:
Today we'll learn about:
Python
Java
JavaScript
Let's code!
- Find the gap. The comment
// Write the for loop below:marks the place between the two existingprintlncalls. The heading is printed above it andLet's code!below it, so the loop goes exactly there. - Write the loop header with the loop variable named
language:for (language in languages). - Put one
printlninside the braces that prints the loop variable. It runs once per element and puts each language on its own line. - Leave
println("Let's code!")below the closing brace so it runs only once, after the last language.
val languages = arrayOf("Python", "Java", "JavaScript")
println("Today we'll learn about:")
// Write the for loop below:
for (language in languages) {
println(language)
}
println("Let's code!")
Common mistakes
- Putting the closing line inside the loop. If
println("Let's code!")sits before the closing brace, it prints after every language instead of once. - Using a different loop variable name. The name
languagein the header and the one inside the body must match. A body that useslanguagesorlangwon't print the elements as intended, or won't compile. - Using the loop variable after the loop. The variable exists only inside the braces. A line below the loop that mentions
languageis a compile error. - Relying on indentation. Kotlin reads braces, not spacing. Indentation helps people see the structure, but the compiler ignores it.
Key takeaways
- The braces of a
forloop mark its body, and the body runs once per element. - Lines after the closing brace run once, after the loop finishes.
- Execution goes from top to bottom, and a loop repeats its body before moving on.
for (x in collection)needs no index, no type and novalorvar.