Start learning free

LESSON 98 OF 285 · CONDITIONAL STATEMENTS - CHALLENGES

Employee of the Year V3 in Go: Three Employees with else if

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:

name1 := "John Doe"
productivity1 := 83
salary1 := 135000
name2 := "Mary Jane"
productivity2 := 133
salary2 := 335000
name3 := "Bob Smith"
productivity3 := 97
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 var winnerName string; var winnerSalary int 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: bonus := winnerSalary * 15 / 100

Join text and a number with strconv.Itoa: fmt.Println("Bonus: $" + strconv.Itoa(bonus))

name1 := "John Doe"
productivity1 := 83
salary1 := 135000
name2 := "Mary Jane"
productivity2 := 133
salary2 := 335000
name3 := "Bob Smith"
productivity3 := 97
salary3 := 210000

var winnerName string
var winnerSalary int
if productivity1 >= productivity2 && productivity1 >= productivity3 {
    winnerName = name1
    winnerSalary = salary1
} else if productivity2 >= productivity3 {
    winnerName = name2
    winnerSalary = salary2
} else {
    winnerName = name3
    winnerSalary = salary3
}
bonus := winnerSalary * 15 / 100
fmt.Println(winnerName)
fmt.Println("Bonus: $" + strconv.Itoa(bonus))

STEP 1 OF 13
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V3 in Go, explained

    Problem overview

    Version 3 adds a third employee (name3, productivity3, salary3) to the two from the previous version, which compared them with a plain if/else. The most productive of the three wins, and a tie goes to whoever is listed first. The program prints the winner's name and a bonus of 15% of their salary, rounded down.

    The data is John Doe (83, 135000), Mary Jane (133, 335000) and Bob Smith (97, 210000). Mary Jane has the highest productivity, so the output is:

    Mary Jane
    Bonus: $50250
    

    Approach: a chain that narrows down the winner

    With two employees, one comparison was enough. With three, a chained if / else if / else works like a cascade of questions:

    1. Is employee 1 at least as productive as both of the others? If yes, employee 1 wins.
    2. If not, employee 1 has lost to someone, so only employees 2 and 3 remain. Is employee 2 at least as productive as employee 3? If yes, employee 2 wins.
    3. Otherwise employee 3 wins.

    Go runs the first branch whose condition is true and skips the rest. Because of that, the second condition is allowed to assume that the first one failed, which is why it compares only employees 2 and 3. As in version 2, the chain only records the winner's name and salary, and the bonus is computed once below it.

    Traced on the starting data: 83 >= 133 is false, so employee 1 is out without evaluating the second half of the &&. Next, 133 >= 97 is true, so the else if branch runs and stores Mary Jane's name and salary. The final else is skipped.

    Three separate if statements would not work here, because more than one of them could be true and the last one to run would overwrite the earlier choices. The else keywords are what make the branches mutually exclusive.

    Step by step

    1. Declare winnerName (a string) and winnerSalary (an int) with var, above the chain, so that the lines below the chain can see them.
    2. Write the first condition: productivity1 >= productivity2 && productivity1 >= productivity3. Each comparison uses >= so a tie keeps employee 1, and && requires both to hold.
    3. Store employee 1's name in the first branch.
    4. Store employee 1's salary there too.
    5. Continue with } else if productivity2 >= productivity3 {. It sits on the line of the closing brace.
    6. Store employee 2's name in that branch.
    7. Store employee 2's salary.
    8. Add the last case, } else {. It needs no condition: employee 3 is the only one left.
    9. Store employee 3's name.
    10. Store employee 3's salary.
    11. Compute the bonus from winnerSalary as winnerSalary * 15 / 100.
    12. Print the name.
    13. Print the bonus line with strconv.Itoa.

    The solution in Go

    name1 := "John Doe"
    productivity1 := 83
    salary1 := 135000
    name2 := "Mary Jane"
    productivity2 := 133
    salary2 := 335000
    name3 := "Bob Smith"
    productivity3 := 97
    salary3 := 210000
    
    var winnerName string
    var winnerSalary int
    if productivity1 >= productivity2 && productivity1 >= productivity3 {
        winnerName = name1
        winnerSalary = salary1
    } else if productivity2 >= productivity3 {
        winnerName = name2
        winnerSalary = salary2
    } else {
        winnerName = name3
        winnerSalary = salary3
    }
    bonus := winnerSalary * 15 / 100
    fmt.Println(winnerName)
    fmt.Println("Bonus: $" + strconv.Itoa(bonus))
    

    The code uses the fmt and strconv packages.

    The details worth knowing

    • else if is just an else that contains an if. Go has no separate elif keyword, and the chain can be as long as needed.
    • && short-circuits. If the left comparison is false, the right one is not evaluated. Here both sides are plain comparisons, so that only matters as a habit.
    • The variables are assigned, not declared, in the branches. Writing winnerName := name1 inside a branch would create a new variable that disappears at the closing brace. The compiler reports winnerName declared and not used for it, and the line below the chain reports undefined: winnerName.
    • Braces and layout. The braces are required, and else must follow the closing brace on the same line.

    Complexity

    At most three comparisons run, so time and extra space are O(1). The chain does not scale gracefully, though: four employees would need a condition with three && parts.

    Edge cases and common mistakes

    • A three-way tie. With productivities 90, 90 and 90 the first condition is true, so employee 1 wins. With 80, 90 and 90 the first condition is false, the second is true, and employee 2 wins, the first of the two tied employees.
    • Using > instead of >=. With strict comparisons, a tie falls through to later branches and the wrong employee can win. With 90, 90, 90 and strict comparisons, Bob Smith would win.
    • Comparing employee 1 only with employee 2. If the first condition omits the check against employee 3, employee 1 wins even when employee 3 is more productive.
    • Re-checking employee 1 in the second condition. It is redundant: reaching that branch already means employee 1 did not win.
    • Mismatched pairs. Each branch must take both the name and the salary from the same employee.

    Testing

    • Productivities 83, 133 and 97 print Mary Jane and Bonus: $50250.
    • Productivities 150, 133 and 97 print John Doe and Bonus: $20250.
    • Productivities 83, 90 and 120 print Bob Smith and Bonus: $31500.
    • Productivities 100, 100 and 100 print John Doe.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free