Start learning free

LESSON 120 OF 338 · WHILE LOOPS & FOR LOOPS

Printing Numbers 3 in Go: A For Loop from 3 to 10

Practice 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 := 2; i < 7; i++ {
	fmt.Println(i)
}

The output of this code is:

2
3
4
5
6

Let's break down this code:

  • The initialization statement is 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

    GO · WHILE LOOPS & FOR LOOPS

    Printing Numbers 3 in Go, explained

    Problem overview

    The task is to print every whole number from 3 through 10, one per line, using a for loop. The first line of output is 3, the last is 10, and both ends are included. That makes eight lines in total.

    Approach: choose the start, the end and the step

    A three-part loop is controlled by an initialization, a condition and a post statement, and all three are yours to tune. For this task:

    • the loop variable starts at the first number to print, 3;
    • the condition must stay true up to and including 10;
    • each pass adds 1.

    The inclusive end is where beginners slip. With i < 10 the loop stops once i reaches 10, so 10 is never printed. Either i <= 10 or i < 11 works, because for whole numbers they mean the same thing. The first one reads more directly from the task ("through 10"), so that is the one used here.

    Tracing the loop

    It helps to follow the variable through the first and last passes:

    1. i := 3 runs once. Is 3 <= 10? Yes, so 3 is printed, and then i++ makes i equal to 4.
    2. The same check and print repeat for 4, 5, 6, 7, 8 and 9.
    3. When i is 10, is 10 <= 10 true? Yes, so 10 is printed, and i becomes 11.
    4. Is 11 <= 10? No, so the loop ends. The body ran eight times: 10 - 3 + 1.

    That count is a useful check. For a loop that goes from a through b in steps of 1, the body runs b - a + 1 times when a <= b. If your output has a different number of lines, one of the bounds is off.

    Step by step

    1. Write the loop header. Declare i with := and the value 3, use i <= 10 as the condition and i++ as the post statement. Go takes no parentheses around these parts, but the body needs braces.
    2. Print inside the body. fmt.Println(i) writes the current value and ends the line, so each number appears on its own line.

    The solution in Go

    for i := 3; i <= 10; i++ {
        fmt.Println(i)
    }
    

    A few Go details:

    • := declares and assigns. i := 3 creates i with type int, inferred from the literal. It only exists inside the loop.
    • fmt.Println prints any value. Given an int it prints the digits, and it always adds a newline. fmt.Print would run the numbers together on one line.
    • i++ is a statement, not an expression. You can write it as the post statement, but not inside another expression such as fmt.Println(i++).

    Edge cases and common mistakes

    • Stopping one short. With i < 10 the output ends at 9.
    • Starting one off. With i := 0, the output begins with 0, 1 and 2, which the task does not want.
    • Forgetting the post statement. Without i++, i stays at 3, the condition is always true, and the program prints 3 forever.
    • Printing text instead of the variable. fmt.Println("i") prints the letter i eight times. Without the quotes, Go uses the variable's value.

    Writing the same loop differently

    Other headers print the same numbers. for i := 3; i < 11; i++ has the end written as "less than 11". You could also count from 0 and print i + 3, but that hides the real range inside the body, so the plain version is easier to read and to change later.

    Testing

    The expected output is the eight lines 3, 4, 5, 6, 7, 8, 9 and 10. If you change the bounds in the editor above to i := 2 and i < 7, you get 2 through 6.

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