Start learning free

LESSON 190 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V3 in C#: Free Shipping with if/else

Practice Read the full explanation

Use an if/else to decide whether to charge shipping based on a free-shipping threshold.

You start with:

        int subtotal = 75;
        int shipping_cost = 10;
        int free_shipping_threshold = 100;

Build:

Subtotal: $75
Shipping: $10
Total: $85

Rules:

  • If subtotal >= free_shipping_threshold, set shipping to 0; otherwise set it to shipping_cost.
  • total = subtotal + shipping.
  • Always include all three lines (Subtotal, Shipping, Total). When shipping is free, the line shows $0: for a subtotal of 120 the lines would be Subtotal: $120, Shipping: $0, Total: $120.
  • Join the three lines with a newline "\n" into one string result, then print it with Console.WriteLine(result).

Decide shipping in an if/else first, then build the three lines.

Declare int shipping; before the if, so it is still visible after the if/else, then assign it in both branches.

In C# "Total: $" + total converts the number to text for you.

        int subtotal = 75;
        int shipping_cost = 10;
        int free_shipping_threshold = 100;

        int shipping;
        if (subtotal >= free_shipping_threshold) {
            shipping = 0;
        } else {
            shipping = shipping_cost;
        }
        int total = subtotal + shipping;
        string line1 = "Subtotal: $" + subtotal;
        string line2 = "Shipping: $" + shipping;
        string line3 = "Total: $" + total;
        string result = line1 + "\n" + line2 + "\n" + line3;
        Console.WriteLine(result);

STEP 1 OF 11
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V3 in C#, explained

    Problem overview

    The cart has a subtotal, a flat shipping_cost and a free_shipping_threshold. If the subtotal reaches the threshold, shipping is free. Otherwise the customer pays the shipping cost. The program prints a three-line receipt: the subtotal, the shipping and the total.

    With a subtotal of 75, a shipping cost of 10 and a threshold of 100, the cart is below the threshold, so shipping is 10 and the total is 85:

    Subtotal: $75
    Shipping: $10
    Total: $85
    

    With a subtotal of 120 the cart qualifies. The lines are Subtotal: $120, Shipping: $0 and Total: $120. The shipping line is always printed, and it shows $0 when shipping is free.

    Approach: decide the shipping first

    The total depends on the shipping, and the shipping depends on a comparison, so the order of work is fixed. First decide shipping with an if/else. Then compute total. Then build the three lines and join them.

    The comparison is subtotal >= free_shipping_threshold. A cart that is exactly on the threshold ships free: a subtotal of 100 gives Shipping: $0. With > instead of >=, that cart would pay 10.

    The decision sets a variable rather than printing, because the shipping value is needed in two places later: in the total and in the second line of the receipt.

    Step by step

    1. Declare shipping without a value. int shipping; creates the variable. One of the two branches will assign it.
    2. Write the if. Its condition is subtotal >= free_shipping_threshold.
    3. Free case. Inside the if, set shipping to 0.
    4. Add the else. It covers every cart under the threshold.
    5. Paid case. Inside the else, set shipping to shipping_cost.
    6. Compute the total. total is subtotal + shipping. When shipping is free it is 0, so the total equals the subtotal.
    7. Build the three lines. line1, line2 and line3 are each a label such as Subtotal: $ joined to a number with +.
    8. Join them into result. Three lines need two newline characters: one between line 1 and line 2, one between line 2 and line 3.
    9. Print result with Console.WriteLine.

    The solution in C#

            int subtotal = 75;
            int shipping_cost = 10;
            int free_shipping_threshold = 100;
    
            int shipping;
            if (subtotal >= free_shipping_threshold) {
                shipping = 0;
            } else {
                shipping = shipping_cost;
            }
            int total = subtotal + shipping;
            string line1 = "Subtotal: $" + subtotal;
            string line2 = "Shipping: $" + shipping;
            string line3 = "Total: $" + total;
            string result = line1 + "\n" + line2 + "\n" + line3;
            Console.WriteLine(result);
    

    The C# details worth a closer look:

    • Declaring without a value is allowed, reading before assigning is not. C# checks that a local variable is assigned on every path before it is read. Both branches of the if/else assign shipping, so the line int total = subtotal + shipping; is accepted. If the else were missing, the compiler would reject that line with Use of unassigned local variable 'shipping'.
    • The same decision in one line. The ternary operator can give shipping its value directly: int shipping = subtotal >= free_shipping_threshold ? 0 : shipping_cost;. The lesson uses if/else because it is the construct being taught, and because it extends to more complicated decisions.
    • + joins text and numbers. With a string on one side, C# turns the int on the other into text. The $ in "Subtotal: $" is an ordinary character in a normal string literal.
    • "\n" is one character. The backslash sequence is the newline character. Console.WriteLine adds its own line ending after the last line, so result doesn't need a newline at its end.
    • Names with underscores. shipping_cost and free_shipping_threshold are valid C# names. The usual C# habit is camelCase for locals, but the lesson keeps the names from the statement.

    Another way to build the lines

    C# also has interpolated strings, written with a leading $ and values in braces. The whole receipt can be one expression: $"Subtotal: ${subtotal}\nShipping: ${shipping}\nTotal: ${total}". For a subtotal of 120 it gives the same three lines as the step-by-step version. The $ at the very front marks the whole string as interpolated; every other $ is plain text that happens to sit before a value in braces. The lesson uses + because each line is built and named separately.

    Edge cases and common mistakes

    • Exactly on the threshold. A subtotal of 100 prints Shipping: $0 and Total: $100.
    • One under the threshold. A subtotal of 99 prints Shipping: $10 and Total: $109. The customer pays more in total than for a subtotal of 100, which follows from the rule.
    • Adding inside the string. "Total: $" + subtotal + shipping doesn't add the numbers. The expression is evaluated left to right, so it joins Total: $75 to 10 and prints Total: $7510. Computing total as an int first, or writing "Total: $" + (subtotal + shipping), gives 85.
    • Leaving out the free line. The receipt always has three lines. An if that prints the shipping line only when it is non-zero would drop it for free shipping.
    • Extra newline at the end. Adding "\n" after line3 would give a blank line after the receipt, because WriteLine already ends the line.
    • Whole dollars only. The variables are int, so the program can't represent cents. A price with cents would need decimal.

    Testing

    • A subtotal of 75 gives Subtotal: $75, Shipping: $10, Total: $85.
    • A subtotal of 120 gives Subtotal: $120, Shipping: $0, Total: $120.
    • A subtotal of 100 gives Subtotal: $100, Shipping: $0, Total: $100.
    • A subtotal of 99 gives Subtotal: $99, Shipping: $10, Total: $109.
    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