Conditional Statements
Programs need to decide: is the user logged in? Is the cart empty? Conditional statements let your code choose a path based on a true/false question.
On this page · 5 min · Beginner
On this page
- In simple words
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
In simple words#
An if statement is a traffic signal: if the light is green, go; else if it is yellow, slow down; else stop. The program picks exactly one road based on the condition.
- if / else
- Run code only when a condition is true.
- switch
- Pick one of many cases by value.
- Condition
- An expression that is true or false.
Learning objectives#
- Write if, else if and else chains
- Use a switch statement for many fixed values
- Use a switch expression to return a value
- Avoid common condition mistakes
Explanation#
if (condition) { ... } runs the block only when the condition is true. Add else if for more checks and else as the fallback. Only the first matching branch runs.
The condition must be a bool. Unlike JavaScript, C# will not treat 0 or an empty string as false — you must write a real comparison.
A switch statement compares one value against many cases. Each case ends with break. Several cases can share one block by stacking them.
A switch expression (value switch { ... }) is the modern, compact form that produces a value. _ is the “anything else” case.
Always use braces { } even for one-line bodies. It prevents bugs when someone adds a second line later.
Syntax#
if (age >= 18) { ... }
else if (age >= 13) { ... }
else { ... }
string size = n switch
{
< 10 => "Small",
< 100 => "Medium",
_ => "Large"
};
Code example: Grades and weekends#
int marks = 76;
if (marks >= 90)
{
Console.WriteLine("Grade A");
}
else if (marks >= 75)
{
Console.WriteLine("Grade B");
}
else
{
Console.WriteLine("Grade C");
}
string day = "Sat";
switch (day)
{
case "Sat":
case "Sun":
Console.WriteLine("Weekend");
break;
default:
Console.WriteLine("Weekday");
break;
}
string label = marks switch
{
>= 90 => "Excellent",
>= 60 => "Good",
_ => "Keep going"
};
Console.WriteLine(label);
Grade B Weekend Good
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- 76 is not >= 90, so the first block is skipped.
- 76 >= 75 is true, so "Grade B" prints and the rest of the chain is skipped.
- The switch matches "Sat"; the stacked case "Sun" shares the same block.
- break ends the case. default handles every other value.
- The switch expression tests the patterns top to bottom and returns the first match: "Good".
Real-world use case#
A delivery API uses a switch expression to turn an order status into a customer message, and an if/else chain to choose the shipping fee by cart total.
Common mistakes#
Checking marks >= 60 before marks >= 90 means nobody ever gets an A. Put the most specific condition first.
In C#, a case with code must end with break (or return). Falling through to the next case is a compile error.
C# needs a bool: write if (count > 0).
Best practices#
- Always use braces
- Prefer switch expressions when you are choosing a value
- Handle the “anything else” case with else, default or _
- Return early to avoid deep nesting
Remember this#
Only the FIRST true branch runs.
Most specific condition first.
switch expression: value switch { pattern => result, _ => fallback }.
Practice exercise#
Your task: Write a switch expression that turns a number 1–7 into the day name (1 = "Mon") and returns "Invalid" for anything else.
Need a hint?
Show solution
int n = 3;
string dayName = n switch
{
1 => "Mon", 2 => "Tue", 3 => "Wed", 4 => "Thu",
5 => "Fri", 6 => "Sat", 7 => "Sun",
_ => "Invalid"
};
Console.WriteLine(dayName); // Wed
Mini quiz#
What is printed?
int t = 30;
if (t > 20) Console.WriteLine("Warm");
else if (t > 25) Console.WriteLine("Hot");
else Console.WriteLine("Cold");
Want more? Practise all C# MCQs · C# interview questions
My notes#
Saved in this browser.