Start learning free

LESSON 99 OF 291 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V3 in Kotlin: if / else if / else for Three

Practice Read the full explanation

Three employees compete for the award. Use a chained if / else if / else to pick the most productive one and print their 15% bonus.

You start with:

        val name1 = "John Doe"
        val productivity1 = 83
        val salary1 = 135000
        val name2 = "Mary Jane"
        val productivity2 = 133
        val salary2 = 335000
        val name3 = "Bob Smith"
        val productivity3 = 97
        val salary3 = 210000

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever of the three employees has the highest productivity.
  • If two or more employees share the highest productivity, the one listed first wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Declare var winnerName: String; var winnerSalary: Int before the if, then use an if / else if / else chain to assign them, so both are still in scope for the prints. Employee 1 wins when its productivity is at least that of both others; if not, employee 2 wins when it is at least employee 3's; otherwise employee 3 wins.

Dividing two whole numbers (Int) drops the fraction, so winnerSalary * 15 / 100 is already rounded down: val bonus = winnerSalary * 15 / 100

+ turns the Int into text: println("Bonus: $" + bonus)

        val name1 = "John Doe"
        val productivity1 = 83
        val salary1 = 135000
        val name2 = "Mary Jane"
        val productivity2 = 133
        val salary2 = 335000
        val name3 = "Bob Smith"
        val productivity3 = 97
        val salary3 = 210000

        var winnerName: String
        var winnerSalary: Int
        if (productivity1 >= productivity2 && productivity1 >= productivity3) {
            winnerName = name1
            winnerSalary = salary1
        } else if (productivity2 >= productivity3) {
            winnerName = name2
            winnerSalary = salary2
        } else {
            winnerName = name3
            winnerSalary = salary3
        }
        val bonus = winnerSalary * 15 / 100
        println(winnerName)
        println("Bonus: $" + bonus)

STEP 1 OF 13
    Ctrl + Enter to check

    KOTLIN · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V3 in Kotlin, explained

    Problem overview

    Now three employees compete. Each has a name, a productivity score and a salary, and the program prints the name and 15% bonus (rounded down) of the one with the highest productivity. If several employees share the highest productivity, the one listed first wins.

    With John Doe at 83 (salary 135000), Mary Jane at 133 (salary 335000) and Bob Smith at 97 (salary 210000), Mary Jane has the highest productivity and the program prints:

    Mary Jane
    Bonus: $50250
    

    The previous version chose between two employees with a single if/else. This one needs a chain.

    Approach

    Think of the choice as a series of questions. Is employee 1 the winner? If so, we're done. If not, employee 1 has lost to someone, so only employees 2 and 3 are left. Is employee 2 at least as productive as employee 3? If so, employee 2 wins. Otherwise employee 3 is the only one left.

    That maps straight onto if / else if / else. Kotlin tests the conditions from top to bottom and runs only the first branch whose condition is true. A later condition is evaluated only when everything above it was false, which is why the second condition needs to compare just employees 2 and 3.

    Step by step

    1. Declare winnerName and winnerSalary before the chain, with their types and no values, exactly as in the previous version.
    2. Test employee 1. The condition is productivity1 >= productivity2 && productivity1 >= productivity3: employee 1 must be at least as productive as both others. && means "and", so both comparisons must hold.
    3. In that branch, store employee 1's name and salary.
    4. Add an else if for employee 2. It only runs when employee 1 didn't win, so one comparison is enough: productivity2 >= productivity3.
    5. Store employee 2's name and salary in that branch.
    6. Finish with a plain else. If employees 1 and 2 both lost, employee 3 is the winner, so this branch needs no condition.
    7. Store employee 3's name and salary.
    8. After the chain, calculate the bonus from winnerSalary and print the name and the bonus line.

    The solution in Kotlin

            val name1 = "John Doe"
            val productivity1 = 83
            val salary1 = 135000
            val name2 = "Mary Jane"
            val productivity2 = 133
            val salary2 = 335000
            val name3 = "Bob Smith"
            val productivity3 = 97
            val salary3 = 210000
    
            var winnerName: String
            var winnerSalary: Int
            if (productivity1 >= productivity2 && productivity1 >= productivity3) {
                winnerName = name1
                winnerSalary = salary1
            } else if (productivity2 >= productivity3) {
                winnerName = name2
                winnerSalary = salary2
            } else {
                winnerName = name3
                winnerSalary = salary3
            }
            val bonus = winnerSalary * 15 / 100
            println(winnerName)
            println("Bonus: $" + bonus)
    

    Kotlin details worth knowing:

    • Exactly one branch runs. Because the chain ends with else, every path assigns both variables, so Kotlin lets you read them afterwards. Without the final else the compiler would report that the variables might not be initialized.
    • && short-circuits. If the left comparison is false, the right one isn't evaluated. It makes no difference here, but it matters when the right side could fail.
    • Order is meaningful. Each later condition relies on the earlier ones being false. Reordering the branches changes the meaning.
    • The bonus line. As before, winnerSalary * 15 / 100 multiplies first and drops the fraction when dividing.

    Following the chain on the lesson's data

    With productivities 83, 133 and 97, the program takes these steps:

    1. The first condition asks whether 83 is at least 133 and at least 97. It is not, so the first branch is skipped.
    2. The else if asks whether 133 is at least 97. It is, so Mary Jane's name and salary are stored, and the final else never runs.
    3. The bonus is 335000 * 15 / 100, which is 50250.

    The same chain written as an expression is also valid Kotlin: val w = if (...) name1 else if (...) name2 else name3 picks the name in one statement. The lesson's version keeps the name and the salary together in each branch, which is why it uses two variables.

    If you drop the last else and only keep the first two branches, the compiler refuses to read the variables afterwards: println(winnerName) fails with error: variable 'winnerName' must be initialized., because nothing assigns them when both conditions are false.

    Edge cases and common mistakes

    • Ties with the first employee. With productivities 90, 80 and 90, employee 1 ties with employee 3 and wins, because the comparisons use >=. The output is John Doe with Bonus: $20250.
    • Ties between the others. With 70, 90 and 90, employee 1 loses, and productivity2 >= productivity3 is true, so Mary Jane wins with Bonus: $50250. Writing > there would hand the win to Bob Smith, whose bonus is $31500.
    • Three-way ties. With all three at 90, the first condition holds and John Doe wins.
    • Comparing only one rival. Checking productivity1 >= productivity2 alone lets employee 1 win even when employee 3 is better.
    • Using separate if statements. Three independent ifs can run more than one branch, and the last one that runs overwrites the earlier winner. The else if chain prevents that.
    • Mixing fields. Each branch must set the name and salary of the same employee.

    Testing

    Try these productivity values in the editor above, keeping the other data:

    productivity1, 2, 3output
    83, 133, 97Mary Jane, Bonus: $50250
    90, 80, 90John Doe, Bonus: $20250
    70, 90, 90Mary Jane, Bonus: $50250
    70, 80, 90Bob Smith, Bonus: $31500
    90, 90, 90John Doe, Bonus: $20250
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free