Start learning free

LESSON 100 OF 291 · CONDITIONAL STATEMENTS - CHALLENGES

Top K Brands V2 in Kotlin: Picking the Higher Rating with if/else

Practice Read the full explanation

Two brands sell the same product. Show the one with the higher rating.

You start with:

        val brand1 = "TechPro"
        val rating1 = 4
        val brand2 = "MegaCorp"
        val rating2 = 5

        var bestBrand: String
        var bestRating: Int

Build:

1. MegaCorp (5)

Rules:

  • Use if/else to pick the winner: brand1 when rating1 > rating2, otherwise brand2.
  • The starter already declares bestBrand and bestRating: assign the winner's name and rating to them inside the branches.
  • Format the result as "1. Brand (rating)" and print it with println(...).

Pick the winner into bestBrand and bestRating first, then build the output text from them.

Kotlin's + on a String and an Int converts the number to text for you.

        val brand1 = "TechPro"
        val rating1 = 4
        val brand2 = "MegaCorp"
        val rating2 = 5

        var bestBrand: String
        var bestRating: Int
        if (rating1 > rating2) {
            bestBrand = brand1
            bestRating = rating1
        } else {
            bestBrand = brand2
            bestRating = rating2
        }
        println("1. " + bestBrand + " (" + bestRating + ")")

STEP 1 OF 5
    Ctrl + Enter to check

    KOTLIN · CONDITIONAL STATEMENTS - CHALLENGES

    Top K Brands V2 in Kotlin, explained

    Problem overview

    Two brands sell the same product, and the program shows the one with the higher rating. The starter code provides both brands and their ratings, plus two variables that are declared but not yet given a value: bestBrand and bestRating.

    With brand1 = "TechPro" (rating 4) and brand2 = "MegaCorp" (rating 5), MegaCorp has the higher rating, so the program prints:

    1. MegaCorp (5)
    

    The rule is: brand1 wins when rating1 > rating2, and otherwise brand2 wins. Version 1 printed a ranked entry for one brand; this version first decides which brand to print.

    Approach

    The decision happens once, and everything after it needs only the winner's name and rating. So the if/else fills bestBrand and bestRating with the winner's values, and a single println after it formats whichever brand won. The formatting code is written once, not once per branch.

    The starter code already declares the two variables, so your work is the branches and the print. They are declared before the if on purpose: a variable created inside a branch's braces can't be seen after the closing brace.

    Step by step

    1. Write the if. The condition is rating1 > rating2. Inside it, set bestBrand to brand1.
    2. Set the rating in the same branch. bestRating = rating1. The winner's name and rating are a pair, so a branch sets both.
    3. Add the else. It runs when rating1 is not greater than rating2, which means brand 2 wins. Set bestBrand to brand2 there.
    4. Set bestRating to rating2 in the else branch.
    5. Print after the if/else. Join "1. ", bestBrand, " (", bestRating and ")" with + and pass the result to println.

    The solution in Kotlin

            val brand1 = "TechPro"
            val rating1 = 4
            val brand2 = "MegaCorp"
            val rating2 = 5
    
            var bestBrand: String
            var bestRating: Int
            if (rating1 > rating2) {
                bestBrand = brand1
                bestRating = rating1
            } else {
                bestBrand = brand2
                bestRating = rating2
            }
            println("1. " + bestBrand + " (" + bestRating + ")")
    

    Kotlin details worth knowing:

    • Deferred initialization. var bestBrand: String declares a variable with a type and no value. The type is required because there is nothing to infer it from. Kotlin lets you read it only after every path has assigned it. Here both branches assign both variables, so the println is accepted. If the else were removed, the compiler would reject the print with error: variable 'bestBrand' must be initialized.
    • The condition. rating1 > rating2 is a Boolean. Unlike some languages, Kotlin's if needs a real Boolean in the parentheses, never a number.
    • String and number. In the final line, every + has a String on its left, so the Int rating is turned into text.
    • if is also an expression. You could write val bestBrand = if (rating1 > rating2) brand1 else brand2, and the same for the rating. This lesson uses the longer form, because it sets the two related values together and shows how branches assign variables.

    Following the code on the lesson's data

    The condition asks whether 4 is greater than 5. It is not, so the if block is skipped and the else block runs. It stores "MegaCorp" in bestBrand and 5 in bestRating. The last line then joins "1. ", MegaCorp, " (", 5 and ")" into 1. MegaCorp (5). Swap the two ratings and the if block runs instead, which prints 1. TechPro (5).

    Whichever branch runs, it assigns exactly the same two variables, and that is what lets the print line work for both outcomes.

    What the compiler checks

    Suppose the else branch sets bestBrand and forgets bestRating. When the if branch runs, both variables are set, but when the else branch runs, bestRating has no value. Kotlin follows every path through the code, finds that second case and refuses to compile the final println:

    error: variable 'bestRating' must be initialized.
    

    This is a useful safety net: a forgotten assignment is caught before the program runs, rather than showing a wrong rating on the page. It also explains why the lesson declares both variables up front and fills them in every branch.

    Edge cases and common mistakes

    • Ties go to brand 2. The condition is a strict >. With two ratings of 5, rating1 > rating2 is false, so else runs and the program prints 1. MegaCorp (5). Changing the comparison to >= would give a tie to brand 1 and change the output, so keep the strict comparison, which is the rule in the task.
    • Brand 1 winning. With ratings 5 and 4, the if branch runs and the output is 1. TechPro (5).
    • Assigning only one of the two variables. If a branch sets bestBrand but not bestRating, the compiler reports that bestRating must be initialized, because one path leaves it without a value.
    • Mixing brands. Setting bestBrand = brand1 together with bestRating = rating2 compiles but prints a rating that doesn't belong to the brand.
    • Printing in each branch. It works for two brands, but the formatting would be copied into both branches. Printing once at the end is the pattern the next versions rely on.
    • Writing = for >. In Kotlin = is assignment and == is comparison; neither is the same as >.

    Testing

    Change the ratings in the editor above and compare:

    rating1, rating2output
    4, 51. MegaCorp (5)
    5, 41. TechPro (5)
    5, 51. MegaCorp (5)
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free