Problem overview
Three employees now compete for the award. The starter data has a name, a productivity and a salary for each: name1, productivity1, salary1 and the same with 2 and 3. The winner is the employee with the highest productivity, and if several share the highest value, the one listed first wins. The program prints the winner's name and a bonus of 15% of their salary, rounded down.
With the starter data, productivities are 83, 133 and 97, so Mary Jane wins and the output is:
Mary Jane
Bonus: $50250
The previous version chose between two employees with one if/else. This version needs a chain with three outcomes.
Approach: ask "is employee 1 the winner?", then narrow down
Employee 1 wins only if their productivity is at least that of employee 2 and at least that of employee 3. That is a condition with two parts joined by &&, and both use >= so that a tie goes to the employee listed first.
If that condition is false, someone has beaten employee 1, so only employees 2 and 3 are left. Employee 2 wins if productivity2 >= productivity3. If that is false too, employee 3 is the winner and needs no condition at all. That is the shape of if / else if / else: each branch can rely on the earlier conditions having failed.
As in version 2, every branch sets the same two variables, winnerName and winnerSalary, so the bonus and the printing are written once after the chain.
Why a chain and not three separate ifs
Three independent if statements, one per employee, would each test their own condition, and more than one could be true at the same time. With a three-way tie, all three would run, and the last one to assign the variables would win, which breaks the tie rule. A chain avoids this: once a branch runs, the rest are skipped, so exactly one employee is chosen.
Following the starter data through the chain: the first condition needs 83 >= 133, which is false, so the && fails and the second comparison is not even evaluated. The else if asks whether 133 >= 97, which is true, so the second branch runs, copies Mary Jane's name and salary, and the final else is skipped.
Step by step
- Declare
winnerNameandwinnerSalarywithout values, before the chain, so the lines after it can read them. - Write the first
if:productivity1is at leastproductivity2and at leastproductivity3, with&&between the two comparisons. Close the brace of the whole chain right away. - Store employee 1's name.
- Store employee 1's salary.
- Add an
else ifthat checksproductivity2 >= productivity3. Employee 1 has already lost to someone, so only employees 2 and 3 are compared. - Store employee 2's name.
- Store employee 2's salary.
- Add the final
else. If employee 1 and then employee 2 have both lost, employee 3 is the winner. - Store employee 3's name.
- Store employee 3's salary.
- Compute
bonusaswinnerSalary * 15 / 100. - Print the winner's name with
endl. - Print the bonus line with
"Bonus: $"andbonus.
The solution in C++
string name1 = "John Doe";
int productivity1 = 83;
int salary1 = 135000;
string name2 = "Mary Jane";
int productivity2 = 133;
int salary2 = 335000;
string name3 = "Bob Smith";
int productivity3 = 97;
int salary3 = 210000;
string winnerName;
int winnerSalary;
if (productivity1 >= productivity2 && productivity1 >= productivity3) {
winnerName = name1;
winnerSalary = salary1;
} else if (productivity2 >= productivity3) {
winnerName = name2;
winnerSalary = salary2;
} else {
winnerName = name3;
winnerSalary = salary3;
}
int bonus = winnerSalary * 15 / 100;
cout << winnerName << endl;
cout << "Bonus: $" << bonus << endl;
Details worth a closer look:
- Branches run in order and only one runs. C++ evaluates the conditions from the top and executes the first branch whose condition is true, then skips the rest. That is why the
else ifcan be short: it is reached only when the first condition was false. - The last
elsehas no condition, and it covers everything not caught above. Because all three variables are assigned in every branch,winnerSalaryis always set before it is read. &&short-circuits. If the left comparison is false, the right one is not evaluated. With plain integers that makes no visible difference, but it is how the operator behaves.
Edge cases and common mistakes
- Three-way ties. With productivity 90 for all three, the first condition is true and employee 1 wins. Their salary of 100000 gives a bonus of 15000.
- A tie between employees 2 and 3. With productivities 50, 70 and 70, the first condition fails, then
70 >= 70is true, so employee 2, listed before employee 3, wins. - Using
>in the second comparison. That sends a 2-versus-3 tie to employee 3, which breaks the tie rule. - Using
||instead of&&. Employee 1 would win by beating just one rival. With productivities 95, 70 and 80 that is still correct, but with 75, 70 and 80 it would wrongly choose employee 1 over employee 3. - Checking only employee 1 against one rival. Comparing only
productivity1 >= productivity2ignores employee 3.
Testing
| Productivities and salaries (employees 1, 2, 3) | Output |
|---|---|
| 83 / 135000, 133 / 335000, 97 / 210000 | Mary Jane, then Bonus: $50250 |
| 90 / 100000, 90 / 200000, 90 / 300000 | A, then Bonus: $15000 |
| 50 / 100000, 70 / 200000, 70 / 300000 | B, then Bonus: $30000 |
| 50 / 100000, 70 / 200000, 80 / 300000 | C, then Bonus: $45000 |
In the last three rows the employees are named A, B and C.
