C# · Basics

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.

Beginner 5 min Lesson 5 of 26Examples target C# 13 on .NET 9 unless stated otherwise.
Before you start:Operators
0/26 in C#
On this page · 5 min · Beginner

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#

C# Illustrative
if (age >= 18) { ... }
else if (age >= 13) { ... }
else { ... }


string size = n switch
{
    < 10 => "Small",
    < 100 => "Medium",
    _ => "Large"
};

Code example: Grades and weekends#

C#Program.cs Illustrative
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);
Expected output
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#

  1. 76 is not >= 90, so the first block is skipped.
  2. 76 >= 75 is true, so "Grade B" prints and the rest of the chain is skipped.
  3. The switch matches "Sat"; the stacked case "Sun" shares the same block.
  4. break ends the case. default handles every other value.
  5. 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#

Wrong order in an else-if chain

Checking marks >= 60 before marks >= 90 means nobody ever gets an A. Put the most specific condition first.

Missing break

In C#, a case with code must end with break (or return). Falling through to the next case is a compile error.

if (count) as in JavaScript

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?
One arm per number, and _ => "Invalid" last.
Show solution
C# Illustrative
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#

Conditional Statements quizPredict the output Question 1 of 3

What is printed?

int t = 30;
if (t > 20) Console.WriteLine("Warm");
else if (t > 25) Console.WriteLine("Hot");
else Console.WriteLine("Cold");
Score so far: 0/0

Want more? Practise all C# MCQs · C# interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.