Problem overview
Three employees now compete: name1, name2, name3, each with a productivity (productivity1 to productivity3) and a salary (salary1 to salary3). The one with the highest productivity wins. If two or more share the highest score, the one listed first wins. The winner gets 15% of their salary as a bonus, rounded down, and the program prints the winner's name followed by Bonus: $ and the amount.
With John Doe (83), Mary Jane (133) and Bob Smith (97), Mary Jane is the best and her bonus is 335000 * 15 / 100 = 50250:
Mary Jane
Bonus: $50250
The previous version chose between two employees with a single if/else. The bonus calculation and printing stay the same here, and only the choosing changes.
Approach: knock out one candidate per test
With two employees, one comparison settled everything. With three, each employee has to beat both of the others. A chain of if / else if / else handles it by eliminating candidates in order:
- Employee 1 wins if their score is at least employee 2's and at least employee 3's.
- Otherwise employee 1 has lost to somebody, so only employees 2 and 3 are left. Employee 2 wins if their score is at least employee 3's.
- Otherwise employee 3 is the winner. No test is needed, because the other two have already been ruled out.
Every comparison uses >=, not >. On a tie the earlier employee must win, and >= lets the earlier one pass the test. As in the previous version, the chain only fills in winnerName and winnerSalary, and the bonus is computed once afterwards.
How the chain runs for the example: 83 >= 133 is false, so the first condition fails. The else if asks 133 >= 97, which is true, so employee 2 is chosen and the else never runs. Once a branch in the chain has run, C# skips all the remaining ones.
Step by step
- Declare
winnerNameandwinnerSalarywithout values, before the chain, so the lines after it can read them. - Write the
if. Its condition joins two comparisons with&&:productivity1 >= productivity2 && productivity1 >= productivity3. Both must be true for employee 1 to win. Close the whole chain's brace right away and fill the branches in afterwards. - In the
if, set the name and the salary to employee 1's. - Add an
else iftestingproductivity2 >= productivity3. Employee 1 already lost, so only 2 and 3 are compared. - In the
else if, set the name and the salary to employee 2's. - Add the final
elseand set employee 3's name and salary. It runs only when employee 1 and then employee 2 both failed their tests. - Compute the bonus with
winnerSalary * 15 / 100and print the name and theBonus: $line.
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;
Console.WriteLine(winnerName);
Console.WriteLine("Bonus: $" + bonus);
Details worth knowing:
else ifis anelsefollowed by anif. C# has no separateelifkeyword. The chain is a nest ofif/elsestatements written flat, and the branches are tested from top to bottom until one is true.&&short-circuits. If the left comparison is false, the right one is not evaluated. It makes no difference for plain comparisons like these, but it is how C# works.- The compiler checks that every path assigns the variables. Because the chain ends with a plain
else, all three paths setwinnerNameandwinnerSalary, and the later lines compile. If the last branch wereelse if (productivity3 >= productivity2)instead, the compiler could not prove that some branch always runs, and printingwinnerNamewould be a compile error. - The condition must be a
bool. C# has no truthy and falsy numbers.
Edge cases and common mistakes
- Ties. With scores 83, 97 and 97, employees 2 and 3 tie, and the program picks Mary Jane (employee 2):
83 >= 97fails, then97 >= 97passes. With 97, 50 and 97, employees 1 and 3 tie and John Doe wins. With three equal scores, John Doe wins. - Comparing employee 1 only with employee 2. A first condition of just
productivity1 >= productivity2picks employee 1 whenever they beat employee 2, even if employee 3 is better. With scores 60, 50 and 97, that version picks employee 1. The chain given above picks employee 3. - Using
>instead of>=. A tie would then go to a later employee instead of the one listed first. - Using
||instead of&&. Employee 1 would win by beating just one of the others. - Testing employee 3 explicitly in the last branch. It is unnecessary: if the first two tests fail, employee 3 has the best score by elimination.
Testing
- Scores 83, 133, 97 give
Mary JaneandBonus: $50250. - Scores 83, 90, 97 give
Bob SmithandBonus: $31500. - Scores 150, 90, 97 give
John DoeandBonus: $20250. - Scores 83, 97, 97 give
Mary JaneandBonus: $50250. - Scores 97, 50, 97 give
John DoeandBonus: $20250.
