Start learning free

LESSON 71 OF 414 · CONDITIONAL STATEMENTS

Is Number Negative in Scala: 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

    SCALA · CONDITIONAL STATEMENTS

    Is Number Negative in Scala, explained

    Problem overview

    The method isNegative takes an integer n and prints one word. It prints negative when n is less than zero, and positive otherwise. It returns nothing; the result is what appears on the console.

    For n = 5 the output is positive. For n = -2 it is negative. For n = 0 it is positive, because zero is not less than zero. The lesson's word positive really means "zero or more".

    Approach

    There are exactly two outcomes, and each has a clear condition, so a single if-else is enough. Test the negative case, n < 0, and let else take everything else. Testing "less than zero" rather than "greater than zero" keeps zero on the right side without any special case.

    Step by step

    1. Write the if with its condition. The check is n < 0. Leave the body empty for the moment.
    2. Print inside the block. Call println("negative"), so the word appears only for negative input.
    3. Add the else. It prints "positive" for every other input. Zero lands here, since 0 < 0 is false.

    The solution in Scala

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

    Some Scala details in this code:

    • Unit as the return type. Unit is what a method returns when it has no useful result to give back. It plays the role that void plays in other languages. The method's job here is the side effect of printing.
    • The block's value. In Scala the last expression of a block is its value. Here that expression is the if-else, and each branch ends in a println, which itself returns Unit. The types line up with the declared return type without any return keyword.
    • Parameter types are written after the name. n: Int declares a 32-bit integer parameter named n.
    • Output text is exact. The words must be lowercase, with no extra spaces, because the printed text is what gets checked.

    A shorter way to write it

    Since an if-else in Scala gives a value, the choice of word can be made first and printed once:

    def isNegative(n: Int): Unit = {
        println(if (n < 0) "negative" else "positive")
    }
    

    It prints the same words. The lesson's version, with a println in each branch, is the better fit for the three steps above. The version with a println in each branch makes the two paths easier to see while you are learning, but both are valid Scala.

    Edge cases and common mistakes

    • Zero. isNegative(0) prints positive. Writing n <= 0 would send it to the wrong branch.
    • The smallest Int. -2147483648 is less than zero, so it prints negative. The method never negates n, so no overflow can occur.
    • Returning instead of printing. The method's type is Unit. The result has to be printed with println.
    • Printing both words. Without the else, the second println would run for every input, negative ones included.
    • Wrong capitalization. "Negative" is not "negative".

    Complexity

    The method does one comparison and one print, so it takes constant time and uses constant extra space, whatever the value of n.

    Testing

    • isNegative(5) prints positive.
    • isNegative(0) prints positive.
    • isNegative(-2) prints negative.
    • isNegative(-1) prints negative.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free