Lesson 09 of 30
Conditional Statements
C# in Visual Studio 2026 — a hands-on guide for developers at every level.
if / else if / else
int score = 73;
if (score >= 90)
Console.WriteLine("Grade: A");
else if (score >= 80)
Console.WriteLine("Grade: B");
else if (score >= 70)
Console.WriteLine("Grade: C");
else
Console.WriteLine("Grade: F");
Ternary Operator
int age = 20;
string status = age >= 18 ? "Adult" : "Minor";
Console.WriteLine(status); // Adult
switch Statement
int day = 3;
switch (day)
{
case 1: Console.WriteLine("Monday"); break;
case 2: Console.WriteLine("Tuesday"); break;
case 3: Console.WriteLine("Wednesday"); break;
default: Console.WriteLine("Other"); break;
}
switch Expression (C# 8+)
The switch expression is a concise alternative that returns a value:
string dayName = day switch
{
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
_ => "Weekend"
};
Console.WriteLine(dayName);
Pattern Matching
object obj = "Hello";
if (obj is string s && s.Length > 3)
Console.WriteLine($"Long string: {s}");