Start learning free

LESSON 98 OF 292 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V3 in Swift: Chained else if

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:

        let name1 = "John Doe"
        let productivity1 = 83
        let salary1 = 135000
        let name2 = "Mary Jane"
        let productivity2 = 133
        let salary2 = 335000
        let name3 = "Bob Smith"
        let productivity3 = 97
        let 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: let bonus = winnerSalary * 15 / 100

Convert the number with String(...) (or use \(...) inside the text): print("Bonus: $" + String(bonus))

        let name1 = "John Doe"
        let productivity1 = 83
        let salary1 = 135000
        let name2 = "Mary Jane"
        let productivity2 = 133
        let salary2 = 335000
        let name3 = "Bob Smith"
        let productivity3 = 97
        let 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
        }
        let bonus = winnerSalary * 15 / 100
        print(winnerName)
        print("Bonus: $" + String(bonus))

STEP 1 OF 13
    Ctrl + Enter to check

    SWIFT · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V3 in Swift, explained

    Problem overview

    Three employees compete. The one with the highest productivity wins, and if several share the top score, the one listed first wins. The program prints the winner's name and a bonus of 15% of their salary, rounded down.

    The starter data holds John Doe (productivity 83, salary 135000), Mary Jane (133, 335000) and Bob Smith (97, 210000). Mary Jane has the highest score, so the output is:

    Mary Jane
    Bonus: $50250
    

    The previous version chose between two employees with an if/else. This one needs three outcomes, so the statement asks for a chain of if, else if and else.

    Approach: eliminate one candidate at a time

    A chain runs the first branch whose condition is true and skips the rest. That makes it a good fit for elimination:

    • Employee 1 wins if their productivity is at least that of employee 2 and at least that of employee 3.
    • If that failed, employee 1 lost to someone, so only employees 2 and 3 remain. Employee 2 wins if they are at least as productive as employee 3.
    • Otherwise employee 3 wins, and the last branch needs no condition.

    Every comparison uses >=. Since employee 1 is checked first, a tie for the top always goes to the lowest-numbered employee. In the second branch, productivity2 >= productivity3 hands a tie between 2 and 3 to employee 2.

    As in version 2, the branches only set winnerName and winnerSalary. The bonus calculation and the prints come once, after the chain.

    Step by step

    1. Declare the winner variables for the name and the salary, with no values, before the chain.
    2. Write the if: employee 1's productivity is at least employee 2's and at least employee 3's, joined with &&.
    3. Store employee 1's name in winnerName.
    4. Store employee 1's salary in winnerSalary.
    5. Add an else if comparing employee 2 with employee 3.
    6. Store employee 2's name.
    7. Store employee 2's salary.
    8. Add the final else. Employee 1 and employee 2 both lost, so employee 3 is the winner.
    9. Store employee 3's name.
    10. Store employee 3's salary.
    11. Compute bonus after the chain as winnerSalary * 15 / 100.
    12. Print the winner's name.
    13. Print the bonus line, converting the number with String().

    Following the starter data through the chain

    The first condition compares employee 1's 83 with employee 2's 133. It is false, so the && never needs to look at employee 3, and the program moves to the else if. There, 133 is at least 97, so the condition is true: winnerName becomes "Mary Jane" and winnerSalary becomes 335000. The final else is skipped, and the lines after the chain print the result shown above.

    A chain with two conditions is enough for three candidates, because each failed condition removes one of them.

    The solution in Swift

            let name1 = "John Doe"
            let productivity1 = 83
            let salary1 = 135000
            let name2 = "Mary Jane"
            let productivity2 = 133
            let salary2 = 335000
            let name3 = "Bob Smith"
            let productivity3 = 97
            let 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
            }
            let bonus = winnerSalary * 15 / 100
            print(winnerName)
            print("Bonus: $" + String(bonus))
    

    The Swift details worth knowing:

    • && and precedence. Comparison operators bind tighter than &&, so a >= b && a >= c needs no parentheses. The right side of && is only evaluated when the left side is true.
    • else if is an if inside an else. The second condition is only tested when the first was false, which is why productivity2 >= productivity3 is enough: employee 1 is already out.
    • Definite initialization. The variables are declared without values, and Swift checks that every branch of the chain assigns both. The chain ends with a plain else, so all paths are covered. A chain that ended with an else if and no else would leave a path where nothing is assigned, and the later lines would not compile.
    • String(bonus). Swift needs text on both sides of +, so the Int is converted first.

    Edge cases and common mistakes

    • Tie between employees 1 and 3. With productivities 97, 50 and 97, employee 1 wins: the program prints John Doe and Bonus: $20250.
    • Tie between employees 2 and 3. With productivities 83, 97 and 97, employee 2 wins: Mary Jane and Bonus: $50250.
    • Employee 3 on top. With productivities 90, 80 and 100, the first condition fails because 90 is below 100. The second fails because 80 is below 100. The else picks Bob Smith, and the output is Bob Smith and Bonus: $31500.
    • Using > in the conditions. A tie at the top would then go to a later employee.
    • Checking only employee 1 against employee 2. A condition like productivity1 >= productivity2 alone lets employee 1 win even when employee 3 is better.
    • Separate if statements instead of a chain. Several independent ifs can overwrite each other's results. The else keywords make the branches exclusive.

    Testing

    • The starter data prints Mary Jane and Bonus: $50250.
    • Productivities 97, 50, 97 print John Doe and Bonus: $20250.
    • Productivities 83, 97, 97 print Mary Jane and Bonus: $50250.
    • Productivities 90, 80, 100 print Bob Smith and Bonus: $31500.
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free