Choosing where the loop starts and stops
A Kotlin for loop is driven by a range. The range decides the first value, the last value and, as the next lesson shows, the step. Changing the range changes what the loop does, so most counting tasks come down to writing the right range.
To print every number from 2 through 6:
for (i in 2..6) {
println(i)
}
It prints these lines:
2
3
4
5
6
The .. range
a..b is a range of whole numbers that includes both ends. It starts at a, counts up by 1 and stops after b. So 2..6 contains 2, 3, 4, 5 and 6, and its length is b - a + 1, here 5.
Because the end is included, "from 2 through 6" translates directly into 2..6, with the numbers copied straight from the wording of the task. This is the form to reach for when a task says "through" or "up to and including".
The same loop with until
until leaves out its right end, so the same numbers can be written as 2 until 7. Both loops print the same five lines. Use whichever matches how the task is worded:
2..6when the last value is given: "through 6".2 until 7when the end is a limit you stay under, such as an array's size.
Empty ranges
If the start is larger than the end, .. gives an empty range and the loop body never runs. No error appears:
for (i in 6..2) {
println(i)
}
println("done")
This prints only done. The .. operator only counts upward. To count down you need downTo, covered in a later lesson.
The exercise: print 3 through 10
The task is to print all numbers from 3 through 10, each on its own line.
- Write the loop. Use the variable
iand the range3..10. Both ends belong to the range, so 10 is printed. The range3 until 10would stop at 9 and lose the last line. - Print the variable. Inside the braces, call
println(i). It prints the current value and ends the line, so each number gets its own line.
The solution in Kotlin
for (i in 3..10) {
println(i)
}
The output is the numbers 3, 4, 5, 6, 7, 8, 9 and 10, one per line: eight lines in all, which is 10 - 3 + 1.
Details worth knowing
- No manual counter. Kotlin moves
ito the next value after each pass. There is noi++to write, and noito declare beforehand. printlnversusprint.println(i)adds a line break after the value.print(i)does not, and the numbers would run together as345678910.- The variable is read-only. You can read
iin the body, buti = 20does not compile. - Negative numbers work.
-2..2gives -2, -1, 0, 1 and 2. - Variables can be bounds. If
nis anInt,1..ncounts from 1 ton, and the loop runs zero times whennis 0 or negative.
Common mistakes
- Off by one at the top. Writing
3 until 10when the task says "through 10". Reading the task for the word "through" or "up to and including" tells you which form to use. - Off by one at the bottom. Starting at 2 or 4 instead of 3. Say the first printed value out loud before writing the range.
- Reversed bounds.
10..3prints nothing at all. - Forgetting to print
i. Printing the text"i"in quotes writes the letter every time instead of the number.
Key takeaways
a..bincludes bothaandb, and counts up by 1.a until bincludesabut notb.- An empty range means the body runs zero times.
- To print 3 through 10:
for (i in 3..10) { println(i) }.