Start learning free

LESSON 96 OF 289 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V2 in C#: if/else to Pick the Winner

Practice Read the full explanation

Two employees are up for the Employee of the Year award. Pick the more productive one and compute 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;

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever employee has the higher productivity.
  • If both have the same productivity, employee 1 (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 if/else to assign them, so both are still in scope for the prints. A tie goes to employee 1, so compare with >=.

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

STEP 1 OF 10
    Ctrl + Enter to check

    C# · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V2 in C#, explained

    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

    1. Declare the two winner variables without values. string winnerName; and int winnerSalary; go before the if. A variable declared inside the braces of a branch exists only until the closing brace, so the lines after the if could not see it.
    2. Write the if with the condition productivity1 >= productivity2. Close its block straight away, so the braces are always balanced while you add the contents.
    3. Inside the if, assign winnerName = name1 and then winnerSalary = salary1. The name and the salary of the winner travel together: set both before leaving the branch.
    4. Add the else. It runs when employee 2 has the strictly higher score.
    5. Inside the else, assign name2 and salary2 to the same two variables, mirroring the if body.
    6. Compute bonus from winnerSalary with the same * 15 / 100 formula as before. Integer division already rounds down.
    7. Print the name and then the Bonus: $ line. The + turns the int into 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. winnerName has no value when it is declared, but both branches assign it, so after the if/else the compiler knows it is set. If you leave out the else and then print winnerName, the code does not compile: Use of unassigned local variable 'winnerName'.
    • Scope. If you declare int winnerSalary = 5; inside the if block and use it afterwards, the compiler reports The name 'winnerSalary' does not exist in the current context. This is why the declarations sit above the if.
    • The condition must be a bool. C# has no "truthy" numbers: if (productivity1) is a compile error. The comparison >= gives a bool.
    • 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 the else branch.
    • 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. winnerSalary has no value at that point.

    Testing

    • John Doe 83 / 135000 against Mary Jane 133 / 335000 gives Mary Jane and Bonus: $50250.
    • John Doe 140 / 135000 against Mary Jane 133 / 335000 gives John Doe and Bonus: $20250.
    • John Doe 100 / 135000 against Mary Jane 100 / 335000 (a tie) gives 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