Start learning free

LESSON 117 OF 338 · WHILE LOOPS & FOR LOOPS

Printing Numbers in Go: Steps of Two and Counting Down

Lesson Read the full explanation

Practice makes perfect! Let's check two more examples of printing numbers using the while loop.


Example 1:

Let's print all the even numbers from 4 through 14:

i := 4
for i <= 14 {
	fmt.Println(i)
	i += 2
}

The output of this program is:

4
6
8
10
12
14

Example 2:

We can also loop in reverse. Let's print all even numbers from 12 to 4 in decreasing order:

i := 12
for i >= 4 {
	fmt.Println(i)
	i -= 2
}

The output of this program is:

12
10
8
6
4

Assignment
Let's print all the odd numbers from 23 to 7 in decreasing order!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 4
    Ctrl + Enter to check

    GO · WHILE LOOPS & FOR LOOPS

    Printing Numbers 2 in Go, explained

    Changing the step and the direction

    The counting loop from the previous lesson adds 1 on every pass. The update does not have to be that. Change the update and you change which numbers appear. Change the comparison and the start and you can count downward. The structure stays the same: a variable, a condition and an update.

    Example 1: even numbers going up

    To print the even numbers from 4 through 14, start at 4 and add 2 each time:

    i := 4
    for i <= 14 {
        fmt.Println(i)
        i += 2
    }
    

    It prints 4, 6, 8, 10, 12 and 14, one per line. Because 4 is even and every step adds an even number, i stays even. The last value, 14, passes the check 14 <= 14. Then i becomes 16, the check fails and the loop ends.

    Example 2: even numbers going down

    To count in reverse, start at the large end, subtract, and flip the comparison:

    i := 12
    for i >= 4 {
        fmt.Println(i)
        i -= 2
    }
    

    This prints 12, 10, 8, 6 and 4. The condition is now i >= 4 because the loop must keep going while the value is still at or above the lower limit. The loop ends when i falls to 2.

    Compare the two examples. When counting up, the condition has an upper limit (<= or <) and the update adds. When counting down, the condition has a lower limit (>= or >) and the update subtracts. If the direction of the update and the direction of the comparison disagree, the loop either never runs or never ends.

    The exercise

    Print all the odd numbers from 23 down to 7, largest first. Decide the three parts first:

    1. Start: the largest number is 23, so declare counter with := and the value 23.
    2. Condition: the smallest number to print is 7, and the loop counts downward, so it runs while counter >= 7.
    3. Body: print counter with fmt.Println.
    4. Update: subtract 2. The distance between two neighbouring odd numbers is 2, and subtracting 2 from an odd number gives an odd number.
    counter := 23
    for counter >= 7 {
        fmt.Println(counter)
        counter -= 2
    }
    

    The output is 23, 21, 19, 17, 15, 13, 11, 9 and 7, which is nine numbers. After 7 is printed, counter becomes 5, the check 5 >= 7 fails and the loop ends.

    Checking the boundaries

    Before you run a counting loop, test its two ends by hand. For the exercise, the first pass checks 23 >= 7, which is true, so 23 is printed. The last wanted value is 7. Does it pass the check? 7 >= 7 is true, so yes. What comes right after? 5, and 5 >= 7 is false, so nothing below 7 is printed. These three checks, first value, last value and first value past the end, catch most off-by-one mistakes.

    The same habit works for Example 1. The last wanted value is 14, and 14 <= 14 is true. The next value is 16, which fails the check. If the task had asked for the numbers below 14, the condition would be i < 14 instead.

    Why the start value matters

    The loop prints only odd numbers because it starts on an odd number and moves in steps of 2. If you started from 24, you would get even numbers, and the loop would stop at 8, since 6 is below the limit. The step does not filter anything. It carries the starting parity along.

    Common mistakes

    • Counting down with <=: for counter <= 7 is false at 23, so nothing is printed.
    • Subtracting without changing the comparison, or adding while counting down. With counter += 2 and counter >= 7 the value grows and the condition stays true, so the loop never ends.
    • Using > 7 when 7 must be printed. That stops at 9.
    • Forgetting that the variable keeps changing after the loop: here counter is 5 when the loop finishes.

    Key takeaways

    • The update decides the step size, and the starting value decides which numbers the step reaches.
    • To count down, start high, use >= or >, and subtract.
    • Always check that the update moves the variable toward the end of the condition.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free