Start learning free

LESSON 69 OF 348 · CONDITIONAL STATEMENTS

Is Number Negative in Kotlin: if and else

Practice Read the full explanation

Create a function that takes a number n as its only argument and prints "negative" to the console if n is less than zero, otherwise prints "positive" to the console.

Example 1:

Input: n = 5
Output: "positive"
Explanation:
5 is greater than or equal to zero, so our function prints "positive" to the console.

Example 2:

Input: n = 0
Output: "positive"
Explanation:
0 is greater than or equal to zero, so our function prints "positive" to the console.

Example 3:

Input: n = -2
Output: "negative"
Explanation:
-2 is less than zero, so our function prints "negative" to the console.

Hints:

You can make use of < operator with an if statement to check if n is negative.

You can add an else statement to handle the case when n is positive.
STEP 1 OF 3
    Ctrl + Enter to check

    KOTLIN · CONDITIONAL STATEMENTS

    Is Number Negative in Kotlin, explained

    Problem overview

    You complete the function isNegative(n: Int), which takes one integer and prints a word. It prints negative when n is less than zero and positive otherwise. The function doesn't return anything. Its job is the side effect of writing a line to the console.

    Three examples show the rules:

    • n = 5 prints positive.
    • n = -2 prints negative.
    • n = 0 prints positive. Zero is not less than zero, so it counts with the non-negative side, even though it isn't strictly positive.

    Approach

    There are exactly two outcomes, and one yes-or-no question decides between them: is n < 0? That is what an if with an else is for. If the answer is yes, print one word. If it is no, print the other. Because every integer is either less than zero or not, one of the two branches always runs and never both.

    Step by step

    1. Write the test. Start with an if whose condition is n < 0 and leave the body empty. Getting the comparison right first is the whole problem. Use <, not <=, so that zero does not match.
    2. Handle the negative case. Inside the if block, call println("negative").
    3. Handle everything else. Add an else branch that calls println("positive"). The else has no condition of its own. It runs for every value that failed the if test: zero and all the positive numbers.

    The solution in Kotlin

    class Solution {
        fun isNegative(n: Int) {
            if (n < 0) {
                println("negative")
            } else {
                println("positive")
            }
        }
    }
    

    A few Kotlin details are worth knowing:

    • No return type. The function header ends with (n: Int) and no : Type. In Kotlin that means the function returns Unit, the "nothing useful" value. You don't write return.
    • println adds the newline. Each call writes the text and moves to the next line, so each call produces exactly one line of output.
    • Strings use double quotes. "negative" is a String. Kotlin's single quotes are for single characters, so 'negative' would not compile.
    • The condition must be a Boolean. n < 0 is one. Kotlin doesn't accept an Int as a condition the way some languages treat zero as false.

    Calling Solution().isNegative(-2) prints negative. Calling it with 5 or with 0 prints positive.

    Edge cases and common mistakes

    • Zero. Writing n <= 0 makes the function print negative for zero, which contradicts Example 2. Zero belongs in the else branch.
    • Two separate if statements. One for n < 0 and another for n >= 0 gives the right output, but it asks two questions where one is enough, and it is easy to leave a gap or an overlap between them. An else guarantees that exactly one branch runs, whatever the value of n.
    • Returning instead of printing. This lesson is checked by what the function prints. A version that returns the word, or one declared with a String return type, prints nothing.
    • Quotes inside the text. The output should be the bare word. The quote marks in the code only mark where the string starts and ends, and they aren't printed.
    • Big magnitudes. Int holds values from about -2.1 billion to 2.1 billion, and the comparison with zero works for all of them. Nothing in this function can overflow.

    Testing

    Try these inputs in the editor above:

    • 5 prints positive.
    • 0 prints positive.
    • -2 prints negative.
    • -1 prints negative, the closest negative value to zero.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free