Problem overview
Three employees compete for the award. Each has a name, a productivity score and a salary. The program finds the one with the highest productivity and prints their name and their bonus, which is 15% of the salary rounded down. If two or more employees share the top score, the one listed first wins.
With John Doe at 83, Mary Jane at 133 and Bob Smith at 97, Mary has the highest score. Her bonus is 335000 * 15 / 100 = 50250:
Mary Jane
Bonus: $50250
The previous version chose between two employees with a single if/else. With three, one comparison isn't enough, so the lesson uses a chain of if, else if and else.
Approach: eliminate one candidate at a time
Think of the chain as a sequence of questions that each remove someone from the race:
- Is employee 1 at least as productive as both of the others? If so, employee 1 wins.
- If not, employee 1 has lost to somebody, so only employees 2 and 3 remain. Is employee 2 at least as productive as employee 3? If so, employee 2 wins.
- Otherwise employee 3 wins, with no further test needed.
The chain stops at the first branch whose condition is true, so the second question is asked only when the first answer was no. That is why the second condition can be as short as productivity2 >= productivity3: employee 1 has already been ruled out.
Every comparison uses >= rather than >. On a tie, the earlier employee must win, and >= lets the earlier employee in the earlier branch claim the tie.
Step by step
- Declare the winner's variables without values:
winner_nameandwinner_salary. They must exist before the chain so the code after it can read them. - Test employee 1. The condition has two comparisons joined by
&&:productivity1must be at leastproductivity2and at leastproductivity3. - Fill in employee 1's name and salary inside that branch. Keep them together so the bonus matches the name.
- Add an
else iffor employee 2 that testsproductivity2 >= productivity3. - Fill in employee 2's name and salary inside it.
- Add the final
else. Reaching it means employees 1 and 2 both lost, so employee 3 is the winner. - Fill in employee 3's name and salary.
- Compute the bonus once, after the chain, as
winner_salary * 15 / 100. - Print the name on one line and
Bonus: $with the amount on the next.
The solution in Rust
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;
let winner_name;
let winner_salary;
if productivity1 >= productivity2 && productivity1 >= productivity3 {
winner_name = name1;
winner_salary = salary1;
} else if productivity2 >= productivity3 {
winner_name = name2;
winner_salary = salary2;
} else {
winner_name = name3;
winner_salary = salary3;
}
let bonus = winner_salary * 15 / 100;
println!("{}", winner_name);
println!("Bonus: ${}", bonus);
Rust details to know:
- Every path assigns both variables. The
let winner_name;form with no value is accepted only because theif, theelse ifand theelseeach assign it. If you left out the finalelse, the compiler would reject the later use as possibly uninitialized, because the chain could end without any branch running. &&short-circuits. Ifproductivity1 >= productivity2is false, the second comparison is never evaluated. Both operands must beboolvalues, and comparing two numbers produces one.else ifis justelsefollowed by anotherif. There is no separate keyword likeelif.- Operands are the same type. All three productivities are
i32through inference, so they compare directly. Rust doesn't compare an integer with a different integer type without a cast.
Edge cases and common mistakes
- Dropping the
&&part. A chain that starts with onlyproductivity1 >= productivity2picks employee 1 whenever they beat employee 2, even if employee 3 is better. With productivities 100, 50 and 120 that chain answers employee 1, while the correct winner is employee 3. - Ties. For 100, 100 and 100 the winner is employee 1. For 50, 90 and 90 it is employee 2, because the first
iffails and employee 2 meets>=against employee 3. - Using
>in any comparison. That hands the tie to a later employee and breaks the rule that the first listed wins. - Repeating the bonus code in each branch. It works, but a single calculation after the chain is shorter and can't drift out of sync between branches.
- Forgetting one of the two assignments in a branch, so the name and salary come from different employees or the compiler complains about an uninitialized variable.
Testing
- Productivities 83, 133, 97 give employee 2 and
Bonus: $50250. - Productivities 100, 100, 100 give employee 1.
- Productivities 50, 90, 90 give employee 2.
- Productivities 50, 60, 90 give employee 3.
- Productivities 120, 90, 100 give employee 1.
