if / else if / elseand the ternary? :switchstatement withcaseandbreak- Switch expressions (C# 8+): concise, return a value, and support patterns like
>= 85.
C# Conditions
if, switch and switch expressions.
✍️ 2 exercises in this lessonKey Takeaways: C# Conditions
Mastering C# Conditions helps you write clean and reliable C# code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
C# Conditions Code Example
C#int marks = 78;
string grade = marks switch
{
>= 85 => "A",
>= 70 => "B",
>= 50 => "C",
_ => "F"
};
Console.WriteLine($"Grade: {grade}");
int hour = 20;
if (hour < 12) Console.WriteLine("Good morning");
else if (hour < 18) Console.WriteLine("Good afternoon");
else Console.WriteLine("Good evening");
Console.WriteLine(marks >= 50 ? "Pass" : "Fail");
Output
Grade: B Good evening Pass
Run this example on your own computer. The "Get Started" lesson shows how to set it up.
C# Conditions Practice Exercises
Check what you learned in “C# Conditions”.
/ 2 solved
Fill in the blank
The discard pattern _ in a switch expression means…
string g = marks switch { >= 85 => "A", => "B" };
✓ Correct!
Not quite. Try again.
Multiple choice
Which C# version introduced switch expressions?
✓ Correct!
Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services