Start learning free

LESSON 97 OF 292 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V2 in Swift: if/else for Two Employees

Practice Read the full explanation

Two employees are up for the Employee of the Year award. Pick the more productive one and compute 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

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever employee has the higher productivity.
  • If both have the same productivity, employee 1 (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 if/else to assign them, so both are still in scope for the prints. A tie goes to employee 1, so compare with >=.

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

        var winnerName: String
        var winnerSalary: Int
        if productivity1 >= productivity2 {
            winnerName = name1
            winnerSalary = salary1
        } else {
            winnerName = name2
            winnerSalary = salary2
        }
        let bonus = winnerSalary * 15 / 100
        print(winnerName)
        print("Bonus: $" + String(bonus))

STEP 1 OF 10
    Ctrl + Enter to check

    SWIFT · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V2 in Swift, explained

    Problem overview

    Two employees compete for the award, each with a name, a productivity score and a salary. The more productive one wins. If the scores are equal, employee 1, the one listed first, wins. The program prints the winner's name, then their bonus: 15% of the salary, rounded down.

    With John Doe at productivity 83 and salary 135000, and Mary Jane at productivity 133 and salary 335000, Mary Jane wins. Her bonus is 335000 * 15 / 100 = 50250:

    Mary Jane
    Bonus: $50250
    

    Version 1 handled one employee and the bonus calculation. This version adds the decision.

    Approach: choose, then calculate once

    The bonus formula is the same for both employees, so the program should not repeat it in each branch. Instead, the if/else only decides who won and copies that employee's name and salary into two variables, winnerName and winnerSalary. After the branches, one bonus calculation and two prints use those variables, whichever branch ran.

    The tie rule fixes the comparison: productivity1 >= productivity2. With >=, equal scores take the first branch, which is employee 1. With a plain >, a tie would fall into the else and employee 2 would win, which breaks the rule.

    Step by step

    1. Declare the two winner variables first, without values: a String for the name and an Int for the salary. They must exist outside the braces, or the code after the if can't see them.
    2. Write the if condition comparing the two productivities with >=.
    3. In the if branch, store employee 1's name in winnerName.
    4. Store employee 1's salary in winnerSalary. Name and salary always travel as a pair.
    5. Add the else, which runs when employee 2 is strictly more productive.
    6. Store employee 2's name in winnerName.
    7. Store employee 2's salary in winnerSalary.
    8. Compute bonus below the if/else: winnerSalary * 15 / 100. Integer division rounds down.
    9. Print the winner's name on its own line.
    10. Print the bonus line. A number can't be joined to text with +, so the number goes through String() first.

    Following the starter data through the code

    The condition compares 83 with 133. It is false, so the program skips the if body and runs the else body: winnerName becomes "Mary Jane" and winnerSalary becomes 335000. From there the code doesn't know or care which branch ran. It reads the two variables, computes 335000 * 15 / 100 = 50250, and prints the two lines shown above.

    Keeping the decision and the calculation apart is a habit worth building: if the bonus rule changes, only one line changes.

    The solution in Swift

            let name1 = "John Doe"
            let productivity1 = 83
            let salary1 = 135000
            let name2 = "Mary Jane"
            let productivity2 = 133
            let salary2 = 335000
    
            var winnerName: String
            var winnerSalary: Int
            if productivity1 >= productivity2 {
                winnerName = name1
                winnerSalary = salary1
            } else {
                winnerName = name2
                winnerSalary = salary2
            }
            let bonus = winnerSalary * 15 / 100
            print(winnerName)
            print("Bonus: $" + String(bonus))
    

    The Swift details worth knowing:

    • Declaring without a value. var winnerName: String reserves the name and the type but gives no value. Swift then checks that every path through the code assigns it before it is read. Here both branches assign both variables, so the later lines compile. If the else were missing, using the variables afterwards would be a compile error: variable 'winnerName' used before being initialized.
    • Scope. A variable created inside braces disappears at the closing brace. If each branch wrote var winnerName = name1 instead of assigning the outer variable, the lines after the if would fail with cannot find 'winnerName' in scope. That is the reason for declaring before the if.
    • String(bonus). It converts the Int to text so that + can join two strings. Interpolation, "Bonus: $\(bonus)", gives the same output.
    • var without mutation. Each variable is assigned exactly once, on whichever path runs. Swift allows a let to be declared first and assigned once on each path, so let would work too; the lesson's code uses var.
    • No parentheses needed. The condition productivity1 >= productivity2 goes straight after if, and the braces are required.

    Edge cases and common mistakes

    • A tie. If both employees have productivity 83, the program prints John Doe and Bonus: $20250: employee 1 wins.
    • Using > instead of >=. It hands ties to employee 2.
    • Name and salary from different employees. Setting winnerName from employee 1 but winnerSalary from employee 2 prints a mismatched result. Keep each branch's two assignments together.
    • Repeating the bonus code in both branches. It works, but it doubles the places to fix. Computing it once after the branches is shorter and safer.
    • Two separate if statements instead of if/else. Writing one if for employee 1 and another for employee 2 leaves a path where neither runs, so Swift can't prove the variables are set and refuses to compile the later lines. The else guarantees that exactly one branch runs.
    • Comparing salaries instead of productivity. The winner depends only on productivity.

    Testing

    • The starter data prints Mary Jane and Bonus: $50250.
    • With both productivities at 83, the program prints John Doe and Bonus: $20250.
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free