Lesson 5 of 34 · Fundamentals
C# if/else, switch and Switch Expressions
Programs become interesting when they can choose what to do. C# gives you if/else for general decisions and two flavours of switch for picking one option out of many — including the compact switch expression that modern C# code uses everywhere.
if, else if, else
int health = 35;
if (health <= 0)
{
Console.WriteLine("Game over");
}
else if (health < 50)
{
Console.WriteLine("Find a potion!");
}
else
{
Console.WriteLine("Fighting fit");
}
The condition in parentheses must be a bool. Unlike some languages, C# will not treat a number as true or false: if (health) does not compile. The conditions are checked from top to bottom and only the first matching branch runs.
For a quick either/or value, use the conditional (ternary) operator condition ? valueIfTrue : valueIfFalse:
string label = health > 0 ? "alive" : "defeated";
The switch statement
When you compare one value against many constants, switch is tidier than a chain of else if:
string command = "north";
switch (command)
{
case "north":
case "up":
Console.WriteLine("You climb the tower.");
break;
case "south":
Console.WriteLine("You return to the gate.");
break;
default:
Console.WriteLine("Unknown command.");
break;
}
Each section ends with break — C# does not let one case silently "fall through" into the next. Stacking case labels (like "north" and "up") shares one section.
Switch expressions
A switch expression chooses a value. It is shorter, and every arm is an expression:
int day = 6;
string name = day switch
{
1 => "Monday",
6 or 7 => "Weekend!",
_ => "Another weekday",
};
The _ (discard) arm matches anything that is left. Arms can use relational patterns and combine them with and, or and not:
int temperature = 23;
string feel = temperature switch
{
< 0 => "freezing",
>= 0 and < 15 => "cold",
< 25 => "pleasant",
_ => "hot",
};
The arms are tried in order, so < 25 only sees temperatures that are at least 15. If no arm matches and there is no _, the program throws an exception at runtime — the compiler warns you about it.
Common mistakes
- Wrong order of conditions: in
if (score >= 60) ... else if (score >= 90)the second branch can never run. Check the most specific case first. - Forgetting the braces: without
{ }only the very next statement belongs to theif. - Missing
_arm: always cover "everything else" in a switch expression.
Assignment
Complete three methods in Program:
Classify(n)returns"negative","zero"or"positive"(useif/else if/else).Grade(score)turns a score from 0 to 100 into a letter with a switch expression: 90 and above is"A", 80–89"B", 70–79"C", 60–69"D"and below 60"F". Scores below 0 or above 100 return"invalid".DayType(day)returns"weekend"for"Saturday"and"Sunday","weekday"for"Monday"to"Friday", and"unknown"for anything else.
If/else and conditions in other languages
- JavaScript if/else Statements and Comparison Operators · JavaScript
- Python If, Elif and Else Statements · Python
- Go if/else and switch Statements · Go
- Java if/else and switch Statements · Java
Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.
Lesson last updated on .