Lesson 46 min read

Conditionals

Teach your code to make decisions

Making Choices in Code

Life is full of decisions. "If it's raining, take an umbrella. Otherwise, wear sunglasses." Your code needs to make decisions too, and that's exactly what conditionals do.

The if statement is the most basic decision-maker. It checks a condition (something that's either true or false) and runs a block of code only when the condition is true. This branching logic is at the heart of algorithms like binary search. You can chain decisions together with else if and provide a fallback with else.

if / else if / else

int temperature = 28;
if (temperature > 35)
{
Console.WriteLine("It's scorching! Stay indoors.");
}
else if (temperature > 25)
{
Console.WriteLine("Nice and warm. Perfect for the beach!");
}
else if (temperature > 10)
{
Console.WriteLine("A bit chilly. Grab a jacket.");
}
else
{
Console.WriteLine("Freezing! Bundle up.");
}
// Nested if
int age = 16;
bool hasLicense = false;
if (age >= 16)
{
if (hasLicense)
Console.WriteLine("You can drive!");
else
Console.WriteLine("Old enough, but get your license first!");
}
Output
Nice and warm. Perfect for the beach!
Old enough, but get your license first!

Switch — The Cleaner Multi-Choice

When you're comparing one value against many options, switch is cleaner than a tower of if/else if blocks. Think of it like a vending machine — you press a button (the value), and it matches to the right slot.

Modern C# has switch expressions (introduced in C# 8) that are even more compact. And with pattern matching, switch becomes incredibly powerful — you can match types, ranges, and even combine conditions.

Switch Statement & Switch Expression

// Classic switch statement
string day = "Wednesday";
switch (day)
{
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
Console.WriteLine($"{day} is a weekday.");
break;
case "Saturday":
case "Sunday":
Console.WriteLine($"{day} is the weekend!");
break;
default:
Console.WriteLine("Not a real day!");
break;
}
// Modern switch expression (much cleaner!)
int score = 87;
string grade = score switch
{
>= 90 => "A",
>= 80 => "B",
>= 70 => "C",
>= 60 => "D",
_ => "F" // _ is the default/discard
};
Console.WriteLine($"Score {score} = Grade {grade}");
Output
Wednesday is a weekday.
Score 87 = Grade B

Pattern Matching in Switch

// Pattern matching with types
object item = 3.14;
string description = item switch
{
int n when n > 0 => $"Positive integer: {n}",
int n => $"Integer: {n}",
double d => $"Decimal number: {d}",
string s => $"Text: {s}",
null => "Nothing here!",
_ => "Something else entirely"
};
Console.WriteLine(description);
// Ternary operator — mini if/else
int hour = 14;
string period = hour < 12 ? "AM" : "PM";
Console.WriteLine($"It's {hour} — that's {period}");
// Nested ternary (use sparingly!)
string timeOfDay = hour < 12 ? "morning" : hour < 17 ? "afternoon" : "evening";
Console.WriteLine($"Good {timeOfDay}!");
Output
Decimal number: 3.14
It's 14 — that's PM
Good afternoon!
Note: 🎯 Switch expressions (with =>) are the modern C# way. They're shorter, return a value directly, and the compiler warns you if you miss a case. Use them over switch statements whenever you're assigning a value based on conditions.

Quick check

What does the _ (discard) pattern mean in a switch expression?
Strings & InterpolationLoops