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
- Declare the winner variables for the name and the salary, with no values, before the chain.
- Write the
if: employee 1's productivity is at least employee 2's and at least employee 3's, joined with&&. - Store employee 1's name in
winnerName. - Store employee 1's salary in
winnerSalary. - Add an
else ifcomparing employee 2 with employee 3. - Store employee 2's name.
- Store employee 2's salary.
- Add the final
else. Employee 1 and employee 2 both lost, so employee 3 is the winner. - Store employee 3's name.
- Store employee 3's salary.
- Compute
bonusafter the chain aswinnerSalary * 15 / 100. - Print the winner's name.
- 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&&, soa >= b && a >= cneeds no parentheses. The right side of&&is only evaluated when the left side is true.else ifis anifinside anelse. The second condition is only tested when the first was false, which is whyproductivity2 >= productivity3is 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 anelse ifand noelsewould leave a path where nothing is assigned, and the later lines would not compile. String(bonus). Swift needs text on both sides of+, so theIntis 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 DoeandBonus: $20250. - Tie between employees 2 and 3. With productivities 83, 97 and 97, employee 2 wins:
Mary JaneandBonus: $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
elsepicks Bob Smith, and the output isBob SmithandBonus: $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 >= productivity2alone lets employee 1 win even when employee 3 is better. - Separate
ifstatements instead of a chain. Several independentifs can overwrite each other's results. Theelsekeywords make the branches exclusive.
Testing
- The starter data prints
Mary JaneandBonus: $50250. - Productivities 97, 50, 97 print
John DoeandBonus: $20250. - Productivities 83, 97, 97 print
Mary JaneandBonus: $50250. - Productivities 90, 80, 100 print
Bob SmithandBonus: $31500.
