What the code is supposed to do
The function printPositiveAndNegative receives an IntArray named nums and prints exactly one line for each element. If the number is below zero, the line is the word negative. Otherwise the line is the number itself. Zero counts as "otherwise", so it is printed as 0.
For [2, -12, 0, 4, -5, 3] the expected output is:
2
negative
0
4
negative
3
The code you start with produces nine lines instead of six:
fun printPositiveAndNegative(nums: IntArray) {
for (num in nums) {
if (num > 0) {
println("positive")
}
println(num)
}
}
What goes wrong
There are two separate mistakes in the loop body.
- The condition is for the wrong numbers.
num > 0is true for the positive numbers, so they get an extra line sayingpositive. The special message was meant for the numbers below zero, and the word was meant to benegative. - The final
println(num)is unconditional. It sits after theif, not inside a branch, so it runs for every element. A positive number therefore produces two lines, the message and the number itself, and a negative number is printed as-12instead ofnegative.
Tracing the first three elements shows the effect. For 2 the condition is true, so the output is positive then 2. For -12 the condition is false and only -12 is printed. For 0 the condition is false as well, since zero is not greater than zero, and only 0 is printed. That matches the nine wrong lines the starting code prints: positive, 2, -12, 0, positive, 4, -5, positive, 3.
Fixing it step by step
- Decide which case gets which output. Below zero prints
negative. Everything else prints the number. Two cases that are exclusive and cover every number call forifandelse. - Change the condition from
num > 0tonum < 0, so that it is true exactly for the negative numbers. - Change the message to
"negative". - Move the number's print into an
elsebranch. The two branches are alternatives, so each element now produces exactly one line.
The fixed code in Kotlin
fun printPositiveAndNegative(nums: IntArray) {
for (num in nums) {
if (num < 0) {
println("negative")
} else {
println(num)
}
}
}
fun mainFunction() {
printPositiveAndNegative(intArrayOf(2, -12, 0, 4, -5, 3))
}
A few details about this code:
- Looping over an array.
for (num in nums)gives each element in order, with no index to manage. IntArrayandintArrayOf.IntArrayis Kotlin's array of primitiveIntvalues, andintArrayOf(...)builds one. It is not the same type asArray<Int>orList<Int>, so a function declared withIntArraydoesn't accept the other two.printlnaccepts any value. It prints a string as it is and turns anIntinto its decimal text, so both branches can use the same function.- Quotes decide what is printed.
println("negative")prints the word, whileprintln(negative)would be a compile error because no variable has that name.
Edge cases and common mistakes
- Zero. Because the condition is a strict
<, zero goes to theelsebranch and is printed as0. Writingnum <= 0would printnegativefor zero, which is wrong for this task. - Empty array. The loop body never runs, so nothing is printed.
- Keeping the old condition. Flipping only the message to
negativewhile keepingnum > 0would label the positive numbers as negative. - Forgetting the
else. Changing the condition and message but leavingprintln(num)after theifstill prints both lines for a negative number:negativeand then-12.
The general lesson is that a statement placed after an if runs on every pass, whatever the condition said. Put it in a branch whenever it should run only for some of the values.