Start learning free

LESSON 121 OF 338 · WHILE LOOPS & FOR LOOPS

Printing Numbers 4 in Go: Odd Numbers with a Step of 2

Practice Read the full explanation

Let's see an example where we change the iteration statement.

We can print all even numbers from 2 through 10:

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

The output of this code is:

2
4
6
8
10

Let's break down this code:

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

  • The condition statement is i <= 10 so we end at number 10. We could've written i < 11 and it would still be correct.

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


Assignment

Let's print all odd numbers from 7 through 23 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 4 in Go, explained

    Problem overview

    The task is to print all the odd numbers from 7 through 23, one per line, using a for loop. The output is 7, 9, 11, 13, 15, 17, 19, 21 and 23: nine lines, each two more than the one before.

    Approach: change the post statement

    The previous printing lessons added 1 on every pass. Here the post statement does the work. If the loop starts at an odd number and adds 2 each time, it only ever lands on odd numbers, so no if is needed to filter anything.

    The loop starts at 7, the first number to print. The step is i += 2, which is shorthand for i = i + 2. The end needs a little care: the last value to print is 23, and the loop reaches it exactly, so i <= 23 includes it. The condition i < 24 would behave the same here.

    Tracing the loop

    Follow the value of i through the passes:

    1. The loop starts with i equal to 7. Is 7 <= 23? Yes, so 7 is printed, and i += 2 makes it 9.
    2. Is 9 <= 23? Yes, so 9 is printed, and i becomes 11. The same happens for 11, 13, 15, 17, 19 and 21.
    3. When i is 23, the check passes, 23 is printed, and i becomes 25.
    4. Is 25 <= 23? No, so the loop ends after nine passes.

    The number of passes is (23 - 7) / 2 + 1, which is 9. Integer division in Go truncates, so for bounds that don't line up exactly, the last value is the last one that still satisfies the condition.

    Step by step

    1. Write the loop with a variable i that starts at 7, runs while i <= 23 and grows by 2 on every pass.
    2. Print i inside the body with fmt.Println, so each value is on its own line.

    The solution in Go

    for i := 7; i <= 23; i += 2 {
        fmt.Println(i)
    }
    

    The details:

    • += works like it does in other C-style languages. i += 2 adds 2 to i in place. It is an assignment statement, so it fits in the post position of the loop header.
    • The parity never changes. Odd plus 2 is odd, so starting at an odd number keeps every value odd. Starting at 8 would print the even numbers 8, 10, 12 and so on instead.
    • The loop checks the condition before each pass. After 23 is printed, i becomes 25, 25 <= 23 is false and the loop ends. The value 25 is never printed.

    Edge cases and common mistakes

    • Using i++. That prints every number from 7 to 23, including the even ones.
    • Starting at the wrong number. Beginning at 6 or 8 produces even numbers all the way through.
    • Using i < 23. The last number, 23, would be missing.
    • Filtering with % when it isn't necessary. Looping through every number and checking i % 2 == 1 gives the same output but does twice as many passes. Choosing the right start and step is simpler. The remainder check is the right tool when the numbers don't follow a regular step.

    Testing

    Run the loop and compare against the nine numbers listed above. As a variation, change the start to 2, the end to 10 and keep the step at 2: the output is 2, 4, 6, 8 and 10.

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