Skip to content
If/Else and Switch
step 1/6

Reading — step 1 of 6

Learn

~3 min readControl Flow

If/Else and Switch

Everything interesting a program does comes down to making decisions. C# gives you two tools: if/else chains for arbitrary conditions, and switch for choosing between known values.

if / else if / else

if (score >= 90) {
    Console.WriteLine("A");
} else if (score >= 80) {
    Console.WriteLine("B");
} else {
    Console.WriteLine("F");
}

Conditions are checked top to bottom; the first true branch runs and the rest are skipped. Comparisons: ==, !=, <, >, <=, >=. Combine with && (and), || (or), ! (not). && and || short-circuit: in a && b, if a is false, b is never even evaluated.

One thing C# does better than C and C++: the condition must be a bool. The infamous if (x = 5) typo — assignment instead of comparison — is a compile error in C#, not a silent bug.

And unlike Java, comparing strings with == compares their contents: "Mon" == "Mon" is true. That is exactly what you want when checking user input.

switch — choosing between known values

When you compare one value against a list of specific candidates, switch reads better than a ladder of else if:

switch (day) {
    case "Mon":
    case "Tue":
    case "Wed":
    case "Thu":
    case "Fri":
        Console.WriteLine("weekday");
        break;
    case "Sat":
    case "Sun":
        Console.WriteLine("weekend");
        break;
    default:
        Console.WriteLine("unknown");
        break;
}

Two things to notice:

  • Stacked case labels (case "Mon": case "Tue": ...) let several values share one body — the idiomatic way to say "any of these".
  • Every non-empty case must end with break (or return). C# refuses to compile silent fall-through from one case body into the next — another whole class of C bugs designed out of the language.

Modern .NET (C# 8+) also has a compact switch expression form: day switch { "Sat" => "weekend", _ => "unknown" }. Know that it exists — but the Mono C# compiler used by this course's grader only supports up to C# 7, so switch expressions will not compile here. In the exercises, stick to if/else or the classic switch shown above.

The trap: forgetting the "none of the above" case

// Trap: no default — unexpected input prints nothing at all
switch (day) {
    case "Sat":
    case "Sun":
        Console.WriteLine("weekend");
        break;
}
// input "Xyz" -> no output -> the grader sees empty stdout and fails you

Always ask: what should happen when the input matches nothing? In an if ladder, that is the final else; in a switch, it is default. Leaving it out does not cause an error — it causes silence, which is worse.

Your exercise

Read a three-letter day abbreviation and print weekday for Mon through Fri, weekend for Sat or Sun, and unknown for anything else. Matching is case-sensitive — the input is exactly Mon, Sat, and so on, so compare against those exact strings.

The grader's hidden test sends Xyz and expects unknown — precisely the trap above. Forget the default case (or the final else) and you print nothing and fail. Watch the output spelling too: all lowercase, exactly weekday, weekend, unknown — a capitalized Weekend fails the character-for-character comparison.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…