Problem overview
Two brands sell the same product, and the program must show the one with the higher rating. The data is brand1 and rating1 for the first brand and brand2 and rating2 for the second. The starter code also declares two variables with no value, bestBrand (a string) and bestRating (an int). The program fills them in and prints one ranked line: 1. Brand (rating).
With TechPro rated 4 and MegaCorp rated 5, MegaCorp wins:
1. MegaCorp (5)
The previous version printed a ranked entry for a single brand. This one adds a decision about which brand it is.
Approach: decide first, print once
The rule is: brand1 wins when rating1 > rating2, and otherwise brand2 wins. The else covers two situations: the second rating is higher, or the ratings are equal. A tie therefore goes to brand2.
Instead of printing inside each branch, each branch just stores the winner's name and rating in bestBrand and bestRating. After the if/else, one print statement formats the winner. The formatting code then exists once, and whichever branch ran, the two variables hold the right values.
For the example, 4 > 5 is false, so the else branch runs. It stores "MegaCorp" and 5, and the print produces 1. MegaCorp (5).
It helps to see why the two branches are enough. For any pair of ratings, exactly one of rating1 > rating2 and its opposite, rating1 <= rating2, is true. The else is the opposite condition written for you, so it never needs a test of its own, and no pair of ratings can reach neither branch or both.
A tie is a case the rules have to decide, because neither brand is strictly better. The lesson's rule is that the second brand takes it. If you wanted ties to go to the first brand, you would change the condition to >= and leave the rest of the code alone.
The two values travel as a pair: whenever bestBrand changes, bestRating must change with it. Keeping them in two variables that are always set in the same branch makes it hard for the name to belong to one brand and the rating to the other.
Step by step
- Write the
ifforrating1 > rating2and, inside it, setbestBrandtobrand1. The variables were already declared in the starter code, so the branches only assign to them. - Still inside the
if, setbestRatingtorating1. The name and the rating of the winner belong together, so set both before leaving the branch. - Add the
elseand setbestBrandtobrand2. - Inside the
else, setbestRatingtorating2. - Print the winner after the
else:"1. ", thenbestBrand, then" (", thenbestRatingand")", all joined with+. The number is converted to text automatically.
The solution in C#
string brand1 = "TechPro";
int rating1 = 4;
string brand2 = "MegaCorp";
int rating2 = 5;
string bestBrand;
int bestRating;
if (rating1 > rating2) {
bestBrand = brand1;
bestRating = rating1;
} else {
bestBrand = brand2;
bestRating = rating2;
}
Console.WriteLine("1. " + bestBrand + " (" + bestRating + ")");
Details worth knowing:
- Declaring without a value.
string bestBrand;is legal, but C# will not let you read the variable until the compiler can prove it has been assigned. Both branches assign both variables, so the print after theif/elsecompiles. - What happens if a branch forgets one. If the
ifbranch assignsbestBrandbut notbestRating, the print line does not compile:Use of unassigned local variable 'bestRating'. C# reports this at compile time, so the mistake can't turn into a silent default value. - The condition is a
bool.rating1 > rating2is true or false. The braces are required here because each branch has two statements. - A shorter form. C# also has the conditional operator,
bestBrand = rating1 > rating2 ? brand1 : brand2;, which picks one of two values in a single expression. For the same data it gives MegaCorp and a best rating of 5. With two values to set, theif/elsekeeps the pair together in one place. - String assignment.
bestBrand = brand1copies the reference to the text. Because strings are immutable, there is no way for one variable to change the other's text afterwards.
Edge cases and common mistakes
- Ties. With both ratings equal to 4, the
ifcondition is false, so theelsebranch runs and the output is1. MegaCorp (4). The lesson's rule puts ties on theelseside. Switching the condition to>=would give a tie to the first brand instead. - Brand 1 wins. With TechPro at 5 and MegaCorp at 3, the output is
1. TechPro (5). - Updating only the name. Setting
bestBrandbut notbestRatingleaves the rating undefined, which the compiler catches as above. - Printing in the branches. It works, but the same formatting is written twice, and a fix in one place can be missed in the other.
- Missing spaces or parentheses in the output. The line must read exactly
1. MegaCorp (5).
Testing
- TechPro 4 against MegaCorp 5 gives
1. MegaCorp (5). - TechPro 5 against MegaCorp 3 gives
1. TechPro (5). - TechPro 4 against MegaCorp 4 gives
1. MegaCorp (4).
