Start learning free

LESSON 120 OF 348 · WHILE LOOPS & FOR LOOPS

Printing a Range of Numbers with a For Loop in Kotlin

Lesson Read the full explanation

We can achieve many different outcomes with for loops.

It's all about tuning the initialization, condition and iteration statements.

For example, we can print all numbers from 2 through 6:

for (i in 2..6) {
	println(i)
}

The output of this code is:

2
3
4
5
6

Let's break down this code:

  • The initialization statement is int i = 2, so we start iterating from number 2.

  • The condition statement is i < 7. We could've written i <= 6 and it would still be correct.

  • The iteration statement is i++, so we increase our number by 1 every time.


Assignment

Let's print all numbers from 3 through 10 using a for loop.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    KOTLIN · WHILE LOOPS & FOR LOOPS

    Printing Numbers 3 in Kotlin, explained

    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..6 when the last value is given: "through 6".
    • 2 until 7 when 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.

    1. Write the loop. Use the variable i and the range 3..10. Both ends belong to the range, so 10 is printed. The range 3 until 10 would stop at 9 and lose the last line.
    2. 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 i to the next value after each pass. There is no i++ to write, and no i to declare beforehand.
    • println versus print. println(i) adds a line break after the value. print(i) does not, and the numbers would run together as 345678910.
    • The variable is read-only. You can read i in the body, but i = 20 does not compile.
    • Negative numbers work. -2..2 gives -2, -1, 0, 1 and 2.
    • Variables can be bounds. If n is an Int, 1..n counts from 1 to n, and the loop runs zero times when n is 0 or negative.

    Common mistakes

    • Off by one at the top. Writing 3 until 10 when 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..3 prints 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..b includes both a and b, and counts up by 1.
    • a until b includes a but not b.
    • An empty range means the body runs zero times.
    • To print 3 through 10: for (i in 3..10) { println(i) }.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free