Problem overview
Two employees are up for the award, each described by a name, a productivity score and a salary: name1, productivity1, salary1 and name2, productivity2, salary2. The more productive one wins. If the scores are equal, employee 1, the one listed first, wins. The winner receives a bonus of 15% of their salary, rounded down.
With John Doe (productivity 83, salary 135000) and Mary Jane (productivity 133, salary 335000), Mary Jane has the higher score. Her bonus is 335000 * 15 / 100 = 50250, so the output is:
Mary Jane
Bonus: $50250
The previous version computed a bonus for one employee. This version adds the decision of who gets it.
Approach: choose, then calculate once
You could write the bonus calculation and the printing twice, once for each employee. A cleaner design separates the two jobs. First decide who the winner is and remember their name and salary in two variables, winnerName and winnerSalary. Then run the bonus calculation and the printing once, using those variables. If the rules for the bonus change later, there is only one place to edit.
The decision is an if/else. The condition is productivity1 >= productivity2. It uses >= rather than > because of the tie rule: when the scores are equal, the condition is true and employee 1 is chosen.
To trace the example by hand: 83 >= 133 is false, so the else branch runs. It stores "Mary Jane" in winnerName and 335000 in winnerSalary. The code after the if/else neither knows nor cares which branch ran. It only reads the two variables. That is the whole benefit of choosing first and calculating after: the later lines stay short and identical for every outcome, however the choice is made.
Now the tie case, with both scores equal to 100. The condition 100 >= 100 is true, so the if branch runs and employee 1 is the winner, exactly as the rules require. Only one of the two branches ever runs, and each of them sets both variables, so the lines after the if/else always find a name and a salary waiting for them.
Step by step
- Declare the two winner variables without values.
string winnerName;andint winnerSalary;go before theif. A variable declared inside the braces of a branch exists only until the closing brace, so the lines after theifcould not see it. - Write the
ifwith the conditionproductivity1 >= productivity2. Close its block straight away, so the braces are always balanced while you add the contents. - Inside the
if, assignwinnerName = name1and thenwinnerSalary = salary1. The name and the salary of the winner travel together: set both before leaving the branch. - Add the
else. It runs when employee 2 has the strictly higher score. - Inside the
else, assignname2andsalary2to the same two variables, mirroring theifbody. - Compute
bonusfromwinnerSalarywith the same* 15 / 100formula as before. Integer division already rounds down. - Print the name and then the
Bonus: $line. The+turns theintinto text.
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 winnerName;
int winnerSalary;
if (productivity1 >= productivity2) {
winnerName = name1;
winnerSalary = salary1;
} else {
winnerName = name2;
winnerSalary = salary2;
}
int bonus = winnerSalary * 15 / 100;
Console.WriteLine(winnerName);
Console.WriteLine("Bonus: $" + bonus);
Details worth knowing:
- Definite assignment. C# will not let you read a local variable that might not have been given a value.
winnerNamehas no value when it is declared, but both branches assign it, so after theif/elsethe compiler knows it is set. If you leave out theelseand then printwinnerName, the code does not compile:Use of unassigned local variable 'winnerName'. - Scope. If you declare
int winnerSalary = 5;inside theifblock and use it afterwards, the compiler reportsThe name 'winnerSalary' does not exist in the current context. This is why the declarations sit above theif. - The condition must be a
bool. C# has no "truthy" numbers:if (productivity1)is a compile error. The comparison>=gives abool. - Braces. The braces are optional for a single statement, but here each branch has two assignments, so they are needed.
Edge cases and common mistakes
- A tie goes to employee 1. With both scores equal to 100, the program prints John Doe and a bonus of 20250. Writing
>instead of>=would give the win to employee 2 on a tie:if (a > b)with both equal to 100 takes theelsebranch. - Setting only one of the two variables. If a branch sets the name but forgets the salary, the bonus is computed from the wrong employee or the code fails to compile.
- Comparing the salaries. The winner is chosen by productivity, not by salary. Mary Jane would win on salary too in the example, so a wrong comparison can hide in this data.
- Calculating the bonus before the
if.winnerSalaryhas no value at that point.
Testing
- John Doe 83 / 135000 against Mary Jane 133 / 335000 gives
Mary JaneandBonus: $50250. - John Doe 140 / 135000 against Mary Jane 133 / 335000 gives
John DoeandBonus: $20250. - John Doe 100 / 135000 against Mary Jane 100 / 335000 (a tie) gives
John DoeandBonus: $20250.
