C# If..Else
Conditional statements let a program choose between different paths depending on a bool condition. C# offers if, else if, else and the compact conditional (ternary) operator ?:.
The if statement
The block after if runs only when the condition is true.
using System;
int temperature = 31;
if (temperature > 30)
{
Console.WriteLine("It's hot today.");
}
Console.WriteLine("Have a nice day!");It's hot today.
Have a nice day!
else and else if
else runs when the condition is false. Chain several conditions with else if; C# tests them from top to bottom and runs only the first matching block.
using System;
int[] scores = { 95, 82, 67, 40 };
foreach (int score in scores)
{
string grade;
if (score >= 90)
grade = "A";
else if (score >= 75)
grade = "B";
else if (score >= 60)
grade = "C";
else
grade = "F";
Console.WriteLine(quot;{score} -> {grade}");
}95 -> A
82 -> B
67 -> C
40 -> F
Because the checks run in order, a score of 95 never reaches the >= 75 test. Order your conditions from most to least specific.
Tip: Braces are optional when a branch has a single statement, as above. Many teams still always use braces, because adding a second line later without them is a classic bug.
Nested conditions
An if can sit inside another if. Often it is clearer to combine the conditions with && instead:
using System;
bool loggedIn = true;
bool isAdmin = false;
if (loggedIn)
{
if (isAdmin)
Console.WriteLine("Welcome to the admin panel.");
else
Console.WriteLine("Welcome back!");
}
else
{
Console.WriteLine("Please log in.");
}
// Same idea, flattened:
if (loggedIn && !isAdmin)
Console.WriteLine("Regular user detected.");Welcome back!
Regular user detected.
The conditional operator ?:
For choosing between two values, the ternary operator is shorter than a full if/else:
condition ? valueIfTrue : valueIfFalse
using System;
int items = 1;
string label = items == 1 ? "item" : "items";
Console.WriteLine(quot;You have {items} {label} in your cart.");
int hour = 20;
Console.WriteLine(hour < 12 ? "Good morning" : hour < 18 ? "Good afternoon" : "Good evening");You have 1 item in your cart.
Good evening
Nesting ternaries works but quickly becomes hard to read; prefer if/else or a switch expression for more than two outcomes.
Pattern matching in conditions
The is keyword tests a value against a pattern. It can check ranges and combine them with and, or and not:
using System;
int age = 15;
char c = 'q';
if (age is >= 13 and <= 19)
Console.WriteLine("Teenager");
if (c is 'a' or 'e' or 'i' or 'o' or 'u')
Console.WriteLine("Vowel");
else
Console.WriteLine("Consonant");
object value = 42;
if (value is int number && number > 40)
Console.WriteLine(quot;Big integer: {number}");Teenager
Consonant
Big integer: 42
Common pitfalls
| Mistake | Problem |
|---|---|
if (x = 5) |
Assignment instead of comparison — does not compile for int |
if (x > 5); |
The stray semicolon is an empty statement; the block below always runs |
Wrong order of else if |
A broad condition first hides the more specific ones |
Test yourself
Given int n = 10;, what does n > 5 ? "big" : "small" evaluate to?
- "small"
- "big"
- true
- 10
In an if / else if chain, how many blocks run at most?
- All matching blocks
- Exactly one, always
- At most one — the first whose condition is true
- Two
Which condition checks that x is between 1 and 10 inclusive using patterns?
- x is 1..10
- x is >= 1 and <= 10
- x in (1, 10)
- x between 1 and 10