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
- Write the
if. The condition israting1 > rating2. Inside it, setbestBrandtobrand1. - Set the rating in the same branch.
bestRating = rating1. The winner's name and rating are a pair, so a branch sets both. - Add the
else. It runs whenrating1is not greater thanrating2, which means brand 2 wins. SetbestBrandtobrand2there. - Set
bestRatingtorating2in theelsebranch. - Print after the
if/else. Join"1. ",bestBrand," (",bestRatingand")"with+and pass the result toprintln.
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: Stringdeclares 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 theprintlnis accepted. If theelsewere removed, the compiler would reject the print witherror: variable 'bestBrand' must be initialized. - The condition.
rating1 > rating2is aBoolean. Unlike some languages, Kotlin'sifneeds a realBooleanin the parentheses, never a number. - String and number. In the final line, every
+has aStringon its left, so theIntrating is turned into text. ifis also an expression. You could writeval 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 > rating2is false, soelseruns and the program prints1. 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
ifbranch runs and the output is1. TechPro (5). - Assigning only one of the two variables. If a branch sets
bestBrandbut notbestRating, the compiler reports thatbestRatingmust be initialized, because one path leaves it without a value. - Mixing brands. Setting
bestBrand = brand1together withbestRating = rating2compiles 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, rating2 | output |
|---|---|
| 4, 5 | 1. MegaCorp (5) |
| 5, 4 | 1. TechPro (5) |
| 5, 5 | 1. MegaCorp (5) |
