Start learning free

LESSON 97 OF 289 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V3 in C#: if / else if / else Chain

Practice Read the full explanation

Three employees compete for the award. Use a chained if / else if / else to pick the most productive one and print their 15% bonus.

You start with:

        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;

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever of the three employees has the highest productivity.
  • If two or more employees share the highest productivity, the one listed first wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Declare string winnerName; int winnerSalary; before the if, then use an if / else if / else chain to assign them, so both are still in scope for the prints. Employee 1 wins when its productivity is at least that of both others; if not, employee 2 wins when it is at least employee 3's; otherwise employee 3 wins.

Dividing two whole numbers (int) drops the fraction, so winnerSalary * 15 / 100 is already rounded down: int bonus = winnerSalary * 15 / 100;

+ turns the int into text: Console.WriteLine("Bonus: $" + bonus);

        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);

STEP 1 OF 13
    Ctrl + Enter to check

    C# · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V3 in C#, explained

    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:

    1. Employee 1 wins if their score is at least employee 2's and at least employee 3's.
    2. 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.
    3. 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

    1. Declare winnerName and winnerSalary without values, before the chain, so the lines after it can read them.
    2. 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.
    3. In the if, set the name and the salary to employee 1's.
    4. Add an else if testing productivity2 >= productivity3. Employee 1 already lost, so only 2 and 3 are compared.
    5. In the else if, set the name and the salary to employee 2's.
    6. Add the final else and set employee 3's name and salary. It runs only when employee 1 and then employee 2 both failed their tests.
    7. Compute the bonus with winnerSalary * 15 / 100 and print the name and the Bonus: $ 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 if is an else followed by an if. C# has no separate elif keyword. The chain is a nest of if/else statements 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 set winnerName and winnerSalary, and the later lines compile. If the last branch were else if (productivity3 >= productivity2) instead, the compiler could not prove that some branch always runs, and printing winnerName would 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 >= 97 fails, then 97 >= 97 passes. 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 >= productivity2 picks 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 Jane and Bonus: $50250.
    • Scores 83, 90, 97 give Bob Smith and Bonus: $31500.
    • Scores 150, 90, 97 give John Doe and Bonus: $20250.
    • Scores 83, 97, 97 give Mary Jane and Bonus: $50250.
    • Scores 97, 50, 97 give John Doe and Bonus: $20250.
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free