Problem overview
Version 3 adds a third employee (name3, productivity3, salary3) to the two from the previous version, which compared them with a plain if/else. The most productive of the three wins, and a tie goes to whoever is listed first. The program prints the winner's name and a bonus of 15% of their salary, rounded down.
The data is John Doe (83, 135000), Mary Jane (133, 335000) and Bob Smith (97, 210000). Mary Jane has the highest productivity, so the output is:
Mary Jane
Bonus: $50250
Approach: a chain that narrows down the winner
With two employees, one comparison was enough. With three, a chained if / else if / else works like a cascade of questions:
- Is employee 1 at least as productive as both of the others? If yes, employee 1 wins.
- If not, employee 1 has lost to someone, so only employees 2 and 3 remain. Is employee 2 at least as productive as employee 3? If yes, employee 2 wins.
- Otherwise employee 3 wins.
Go runs the first branch whose condition is true and skips the rest. Because of that, the second condition is allowed to assume that the first one failed, which is why it compares only employees 2 and 3. As in version 2, the chain only records the winner's name and salary, and the bonus is computed once below it.
Traced on the starting data: 83 >= 133 is false, so employee 1 is out without evaluating the second half of the &&. Next, 133 >= 97 is true, so the else if branch runs and stores Mary Jane's name and salary. The final else is skipped.
Three separate if statements would not work here, because more than one of them could be true and the last one to run would overwrite the earlier choices. The else keywords are what make the branches mutually exclusive.
Step by step
- Declare
winnerName(astring) andwinnerSalary(anint) withvar, above the chain, so that the lines below the chain can see them. - Write the first condition:
productivity1 >= productivity2 && productivity1 >= productivity3. Each comparison uses>=so a tie keeps employee 1, and&&requires both to hold. - Store employee 1's name in the first branch.
- Store employee 1's salary there too.
- Continue with
} else if productivity2 >= productivity3 {. It sits on the line of the closing brace. - Store employee 2's name in that branch.
- Store employee 2's salary.
- Add the last case,
} else {. It needs no condition: employee 3 is the only one left. - Store employee 3's name.
- Store employee 3's salary.
- Compute the bonus from
winnerSalaryaswinnerSalary * 15 / 100. - Print the name.
- Print the bonus line with
strconv.Itoa.
The solution in Go
name1 := "John Doe"
productivity1 := 83
salary1 := 135000
name2 := "Mary Jane"
productivity2 := 133
salary2 := 335000
name3 := "Bob Smith"
productivity3 := 97
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
}
bonus := winnerSalary * 15 / 100
fmt.Println(winnerName)
fmt.Println("Bonus: $" + strconv.Itoa(bonus))
The code uses the fmt and strconv packages.
The details worth knowing
else ifis just anelsethat contains anif. Go has no separateelifkeyword, and the chain can be as long as needed.&&short-circuits. If the left comparison is false, the right one is not evaluated. Here both sides are plain comparisons, so that only matters as a habit.- The variables are assigned, not declared, in the branches. Writing
winnerName := name1inside a branch would create a new variable that disappears at the closing brace. The compiler reportswinnerName declared and not usedfor it, and the line below the chain reportsundefined: winnerName. - Braces and layout. The braces are required, and
elsemust follow the closing brace on the same line.
Complexity
At most three comparisons run, so time and extra space are O(1). The chain does not scale gracefully, though: four employees would need a condition with three && parts.
Edge cases and common mistakes
- A three-way tie. With productivities 90, 90 and 90 the first condition is true, so employee 1 wins. With 80, 90 and 90 the first condition is false, the second is true, and employee 2 wins, the first of the two tied employees.
- Using
>instead of>=. With strict comparisons, a tie falls through to later branches and the wrong employee can win. With 90, 90, 90 and strict comparisons, Bob Smith would win. - Comparing employee 1 only with employee 2. If the first condition omits the check against employee 3, employee 1 wins even when employee 3 is more productive.
- Re-checking employee 1 in the second condition. It is redundant: reaching that branch already means employee 1 did not win.
- Mismatched pairs. Each branch must take both the name and the salary from the same employee.
Testing
- Productivities 83, 133 and 97 print
Mary JaneandBonus: $50250. - Productivities 150, 133 and 97 print
John DoeandBonus: $20250. - Productivities 83, 90 and 120 print
Bob SmithandBonus: $31500. - Productivities 100, 100 and 100 print
John Doe.
