Start learning free

LESSON 127 OF 338 · LOOPS & CONDITIONALS

Print Even Odd in Go: if and else Inside a For Loop

Practice Read the full explanation

Given a positive integer n, for each number from 0 to n - 1, print "odd" if the number is odd or the number itself if it is even.

Example:

Input: n = 6
Output (console):
0
odd
2
odd
4
odd


Note:

You have to print to the console, your function shouldn't return anything.

STEP 1 OF 3
    Ctrl + Enter to check

    GO · LOOPS & CONDITIONALS

    Print Even Odd in Go, explained

    Problem overview

    The function printEvenOdd takes a positive integer n. For every number from 0 to n - 1, it prints one line: the word odd when the number is odd, and the number itself when it is even. Nothing is returned.

    For n = 6 the numbers are 0 to 5. The output is 0, odd, 2, odd, 4, odd, one item per line. The loop never reaches 6, because the range stops at n - 1.

    Approach: one loop, one decision per number

    Every number in the range needs exactly one line of output, and the line depends on a yes-or-no question: is the number odd? That is a loop with an if/else inside it. Both branches print, so each pass produces exactly one line.

    To tell odd from even, use the remainder after dividing by 2. Odd numbers leave remainder 1 and even numbers leave 0. For the non-negative numbers here, testing for remainder 1 is enough.

    Step by step

    1. Loop over the range. A for loop with a variable i that starts at 0 and continues while i < n covers exactly 0, 1, ..., n - 1. The post statement is i++.
    2. Handle the odd numbers. Inside the loop, an if checks i % 2 == 1. In that branch, fmt.Println("odd") prints the word.
    3. Handle the rest. The else branch runs for the even numbers, and fmt.Println(i) prints the number itself. Note the difference between the two prints: one has quotes around a word, the other has the variable.

    The solution in Go

    func printEvenOdd(n int) {
        for i := 0; i < n; i++ {
            if i % 2 == 1 {
                fmt.Println("odd")
            } else {
                fmt.Println(i)
            }
        }
    }
    

    The Go details:

    • else sits on the same line as the closing brace. Writing } and then else on the next line is a syntax error, because Go inserts a semicolon at the end of the line.
    • fmt.Println accepts different types. The same function prints a string and an int, so both branches can use it.
    • No ternary operator. Go has no condition ? a : b, so a two-way choice is written as if/else.
    • The remainder operator % works only on integers, and there is no automatic conversion in Go, so i and 2 are both int here.

    Following the loop by hand

    With n = 4, the loop makes four passes. For i = 0, the remainder is 0, so the else branch prints 0. For i = 1, the remainder is 1, so the word is printed. For i = 2, the number 2 is printed, and for i = 3, the word again. Then i becomes 4, 4 < 4 is false and the loop stops. Each pass takes exactly one of the two branches, never both and never neither, which is why the output has exactly n lines.

    Complexity

    Time: O(n). There is one pass per number, and each pass does a constant amount of work.

    Space: O(1). No data structure grows with n.

    Edge cases and common mistakes

    • Zero is even. 0 % 2 is 0, so the first line of output is the number 0, not the word.
    • The upper bound. i <= n would add one extra line for n itself. With n = 6 that adds a final 6.
    • Quotes mixed up. Printing "i" in the else branch gives the letter instead of the number, and printing odd without quotes does not compile, because Go looks for a variable called odd.
    • Two separate if statements. Using one for odd and another for even also works, but else says the two cases are opposites and is shorter.
    • n = 1. The only number is 0, so the output is a single line, 0.

    Testing

    • n = 6 prints 0, odd, 2, odd, 4, odd.
    • n = 1 prints 0.
    • n = 2 prints 0, then odd.
    • n = 4 prints 0, odd, 2, odd.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free