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
- Declare the two winner variables first, without values: a
Stringfor the name and anIntfor the salary. They must exist outside the braces, or the code after theifcan't see them. - Write the
ifcondition comparing the two productivities with>=. - In the
ifbranch, store employee 1's name inwinnerName. - Store employee 1's salary in
winnerSalary. Name and salary always travel as a pair. - Add the
else, which runs when employee 2 is strictly more productive. - Store employee 2's name in
winnerName. - Store employee 2's salary in
winnerSalary. - Compute
bonusbelow theif/else:winnerSalary * 15 / 100. Integer division rounds down. - Print the winner's name on its own line.
- Print the bonus line. A number can't be joined to text with
+, so the number goes throughString()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: Stringreserves 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 theelsewere 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 = name1instead of assigning the outer variable, the lines after theifwould fail withcannot find 'winnerName' in scope. That is the reason for declaring before theif. String(bonus). It converts theIntto text so that+can join two strings. Interpolation,"Bonus: $\(bonus)", gives the same output.varwithout mutation. Each variable is assigned exactly once, on whichever path runs. Swift allows aletto be declared first and assigned once on each path, soletwould work too; the lesson's code usesvar.- No parentheses needed. The condition
productivity1 >= productivity2goes straight afterif, and the braces are required.
Edge cases and common mistakes
- A tie. If both employees have productivity 83, the program prints
John DoeandBonus: $20250: employee 1 wins. - Using
>instead of>=. It hands ties to employee 2. - Name and salary from different employees. Setting
winnerNamefrom employee 1 butwinnerSalaryfrom 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
ifstatements instead ofif/else. Writing oneiffor 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. Theelseguarantees that exactly one branch runs. - Comparing salaries instead of productivity. The winner depends only on productivity.
Testing
- The starter data prints
Mary JaneandBonus: $50250. - With both productivities at 83, the program prints
John DoeandBonus: $20250.
