Conditional Statements
Learn if, else if, else, switch statements, and the switch expression in C#.
if / else if / else
C# evaluates if/else if/else conditions strictly in order, from top to bottom, and executes only the first block whose condition is true — even if a later condition would also technically be true, it is never checked once an earlier one matches.
int score = 72;
if (score >= 90) Console.WriteLine("Grade: A");else if (score >= 70) Console.WriteLine("Grade: B");else if (score >= 50) Console.WriteLine("Grade: C");else Console.WriteLine("Grade: F");Click Run to see what this code prints.
Why Curly Braces Matter
C# allows omitting curly braces { } when an if (or loop) body is exactly one statement, as shown above — but this is a common source of subtle bugs when code is edited later, since only the very next statement belongs to the if, regardless of indentation.
A Classic Trap
if (score >= 90) Console.WriteLine("Grade: A"); Console.WriteLine("Great job!"); // NOT part of the if — always runs!Always Safe with Braces
if (score >= 90){ Console.WriteLine("Grade: A"); Console.WriteLine("Great job!"); // correctly part of the if}Many professional C# style guides require curly braces on every if/else/loop, even single-statement ones, specifically to prevent this exact class of bug when code is modified later by someone in a hurry.
Nested Conditionals
Conditionals can nest inside one another for more complex logic, though deeply nested conditionals are widely considered a readability smell worth refactoring (often into a switch expression, guard clauses, or extracted methods, covered later in this course).
int age = 25;bool hasLicense = true;
if (age >= 18){ if (hasLicense) { Console.WriteLine("Can drive."); } else { Console.WriteLine("Old enough, but needs a license."); }}else{ Console.WriteLine("Too young to drive.");}The same logic, flattened with combined conditions, is often clearer:
if (age >= 18 && hasLicense) Console.WriteLine("Can drive.");else if (age >= 18) Console.WriteLine("Old enough, but needs a license.");else Console.WriteLine("Too young to drive.");The switch Statement
A switch statement compares one value against several possible cases — cleaner than a long chain of else if statements all checking the same variable.
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("Another day"); break;}Click Run to see what this code prints.
Fall-Through & Multiple Cases
Unlike C or JavaScript, C# does NOT allow implicit fall-through from one case to the next — each non-empty case must end with break, return, or another jump statement, or the compiler refuses to build. Multiple case labels can still share one body by stacking them.
int day = 6;switch (day){ case 6: case 7: Console.WriteLine("Weekend!"); break; default: Console.WriteLine("Weekday"); break;}C's (and originally C++'s) implicit switch fall-through is widely regarded as one of the most infamous historical language design mistakes, responsible for countless real-world bugs from a missing break — C# deliberately closed this loophole by requiring an explicit jump statement at the end of every non-empty case.
The switch Expression
Modern C# (8.0+) offers a more concise `switch` expression that returns a value directly, using => arrows instead of case:/break;, avoiding repetitive boilerplate entirely.
int day = 3;string dayName = day switch{ 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 or 7 => "Weekend", _ => "Invalid day"};Console.WriteLine(dayName);Click Run to see what this code prints.
If every branch of your logic just produces a value to assign or return, a switch expression is shorter, has no risk of a missing break, and is less error-prone than a traditional switch statement.
Pattern Matching in switch (A Preview)
Both switch statements and switch expressions can match on far more than exact values — types, ranges, and structural shapes. This is a deep topic covered in its own dedicated Pattern Matching lesson later in this course, but a small preview here shows the direction C# is heading.
object value = 42;
string description = value switch{ int n when n < 0 => "Negative number", int n when n == 0 => "Zero", int n => $"Positive number: {n}", string s => $"A string: {s}", null => "Nothing at all", _ => "Some other type"};Console.WriteLine(description); // Positive number: 42Common Beginner Mistakes
Only the single next statement belongs to an if without braces — a second indented line silently runs unconditionally, regardless of how it looks visually.
Unlike some languages, this is a compile error in C# (good!) rather than a silent fall-through bug — but it still surprises developers coming from JavaScript or C.
Beyond 2-3 levels of nesting, code becomes hard to follow — flattening with combined conditions, early returns, or a switch expression usually reads far more clearly.
FAQs
It's the discard pattern, matching anything not covered by earlier cases — equivalent to `default` in a traditional switch statement.
Yes — since C# 9, pattern combinators like `6 or 7` or `> 0 and < 100` let you combine conditions directly in a case label, without needing separate stacked cases or a when clause for simple combinations.
Not always — a switch expression is ideal when every branch simply produces a value; a traditional switch statement is still the right tool when each branch needs to run multiple, more complex statements rather than just return one value.
Only explicitly, via goto case — a rarely used, deliberately visible escape hatch, unlike C's implicit and easy-to-forget fall-through.
Key Takeaways
- if/else if/else evaluates conditions strictly in order, running only the first matching branch.
- Always use curly braces, even for single-statement bodies, to avoid a well-known class of editing bugs.
- switch statements match a value against multiple cases and require an explicit break (or other jump) per case — no implicit fall-through, unlike C or JavaScript.
- switch expressions are a concise, modern alternative that return a value directly, and support pattern matching for more than simple equality.
Summary
Conditional statements — if/else, switch statements, and switch expressions — are how every C# program actually makes decisions, and understanding the subtle brace and fall-through rules will save you from real, easy-to-miss bugs. Next, you'll learn how to repeat actions using loops.