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
- Loop over the range. A
forloop with a variableithat starts at 0 and continues whilei < ncovers exactly 0, 1, ...,n - 1. The post statement isi++. - Handle the odd numbers. Inside the loop, an
ifchecksi % 2 == 1. In that branch,fmt.Println("odd")prints the word. - Handle the rest. The
elsebranch runs for the even numbers, andfmt.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:
elsesits on the same line as the closing brace. Writing}and thenelseon the next line is a syntax error, because Go inserts a semicolon at the end of the line.fmt.Printlnaccepts different types. The same function prints a string and anint, so both branches can use it.- No ternary operator. Go has no
condition ? a : b, so a two-way choice is written asif/else. - The remainder operator
%works only on integers, and there is no automatic conversion in Go, soiand2are bothinthere.
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 % 2is 0, so the first line of output is the number 0, not the word. - The upper bound.
i <= nwould add one extra line fornitself. Withn = 6that adds a final6. - Quotes mixed up. Printing
"i"in the else branch gives the letter instead of the number, and printingoddwithout quotes does not compile, because Go looks for a variable calledodd. - Two separate
ifstatements. Using one for odd and another for even also works, butelsesays 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 = 6prints 0, odd, 2, odd, 4, odd.n = 1prints 0.n = 2prints 0, then odd.n = 4prints 0, odd, 2, odd.