Start learning free

LESSON 98 OF 289 · CONDITIONAL STATEMENTS - CHALLENGES

Top K Brands V2 in C#: Better of Two with if/else

Practice Read the full explanation

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

You start with:

        string brand1 = "TechPro";
        int rating1 = 4;
        string brand2 = "MegaCorp";
        int rating2 = 5;

        string bestBrand;
        int bestRating;

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 Console.WriteLine(...).

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

C#'s + on a string and an int converts the integer to text for you.

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

STEP 1 OF 5
    Ctrl + Enter to check

    C# · CONDITIONAL STATEMENTS - CHALLENGES

    Top K Brands V2 in C#, explained

    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

    1. Write the if for rating1 > rating2 and, inside it, set bestBrand to brand1. The variables were already declared in the starter code, so the branches only assign to them.
    2. Still inside the if, set bestRating to rating1. The name and the rating of the winner belong together, so set both before leaving the branch.
    3. Add the else and set bestBrand to brand2.
    4. Inside the else, set bestRating to rating2.
    5. Print the winner after the else: "1. ", then bestBrand, then " (", then bestRating and ")", 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 the if/else compiles.
    • What happens if a branch forgets one. If the if branch assigns bestBrand but not bestRating, 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 > rating2 is 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, the if/else keeps the pair together in one place.
    • String assignment. bestBrand = brand1 copies 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 if condition is false, so the else branch runs and the output is 1. MegaCorp (4). The lesson's rule puts ties on the else side. 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 bestBrand but not bestRating leaves 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).
    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