Aral Code

Control Flow Lesson 7 of 15

C# if, else, switch and Switch Expressions

Make decisions in C# with if, else if, else, the switch statement, modern switch expressions with patterns, and the ternary operator.

Decisions let your program choose what to do. C# has if/else, the classic switch statement, and modern switch expressions that make many decisions short and readable.

if, else if, else

int grade = 87;
string descriptor;

if (grade >= 90)
{
    descriptor = "Outstanding";
}
else if (grade >= 85)
{
    descriptor = "Very Satisfactory";
}
else if (grade >= 80)
{
    descriptor = "Satisfactory";
}
else if (grade >= 75)
{
    descriptor = "Fairly Satisfactory";
}
else
{
    descriptor = "Did Not Meet Expectations";
}

Console.WriteLine($"{grade}: {descriptor}");

Output

87: Very Satisfactory

C# style usually puts each brace on its own line. Only the first true branch runs, so the highest range comes first.

Combining conditions

int age = 19;
bool hasVoterId = true;
decimal total = 1500m;
bool isMember = false;

if (age >= 18 && hasVoterId)
{
    Console.WriteLine("You can vote.");
}

if (total >= 1000m || isMember)
{
    Console.WriteLine("Free delivery!");
}

Output

You can vote.
Free delivery!

The switch statement

string day = "Fri";
switch (day)
{
    case "Mon":
        Console.WriteLine("Flag ceremony");
        break;
    case "Fri":
        Console.WriteLine("Coding club");
        break;
    case "Sat":
    case "Sun":
        Console.WriteLine("Weekend!");
        break;
    default:
        Console.WriteLine("Regular class");
        break;
}

Output

Coding club

C# switch works with strings. Unlike C and Java, C# doesn't allow accidental fall-through: each case with code must end with break (or return), otherwise it's a compile error. Empty stacked cases, like "Sat" and "Sun", are allowed.

Switch expressions

A switch expression produces a value in a compact form:

int choice = 2;
string item = choice switch
{
    1 => "Pandesal",
    2 => "Kape",
    3 => "Itlog",
    _ => "Unknown"
};
Console.WriteLine($"You chose {item}");

Output

You chose Kape

_ is the "anything else" case, like default.

Patterns: ranges in a switch

Switch expressions can match ranges with relational patterns:

string Describe(int g) => g switch
{
    >= 90 => "Outstanding",
    >= 85 => "Very Satisfactory",
    >= 80 => "Satisfactory",
    >= 75 => "Fairly Satisfactory",
    _ => "Did Not Meet Expectations"
};

foreach (int g in new[] { 95, 86, 71 })
{
    Console.WriteLine($"{g}: {Describe(g)}");
}

Output

95: Outstanding
86: Very Satisfactory
71: Did Not Meet Expectations

This is much shorter than the if/else if chain. string Describe(int g) => ... is a short method; you'll learn methods soon.

Combining patterns

int month = 4;
string season = month switch
{
    12 or 1 or 2 => "Amihan (cool dry)",
    >= 3 and <= 5 => "Tag-init (hot dry)",
    >= 6 and <= 11 => "Habagat (rainy)",
    _ => "Invalid month"
};
Console.WriteLine(season);

Output

Tag-init (hot dry)

The ternary operator

int items = 1;
Console.WriteLine($"{items} {(items == 1 ? "item" : "items")}");

Output

1 item

Inside an interpolated string, wrap the ternary in parentheses, because : normally starts a format like {x:F2}.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Can a C# switch use strings?
2. What does _ mean in a switch expression?
3. What happens if a case with code has no break in C#?
4. Which pattern matches values from 80 to 89?

By Sir Reyel · Last updated 2026-12-04