C# Conditional Statements

Control the flow of your program with conditional logic

if Statement

The if statement executes code if a specified condition is true.

if Statement Syntax
if (condition)
{
    // Code to execute if condition is true
}
if Statement Example
int temperature = 25;

if (temperature > 20)
{
    Console.WriteLine("It's warm outside.");
}

if-else Statement

The if-else statement executes one block of code if a condition is true, and another block if it's false.

if-else Syntax
if (condition)
{
    // Code to execute if condition is true
}
else
{
    // Code to execute if condition is false
}
if-else Example
int age = 17;

if (age >= 18)
{
    Console.WriteLine("You are an adult.");
}
else
{
    Console.WriteLine("You are a minor.");
}

if-else if-else Statement

Use if-else if-else to test multiple conditions.

if-else if-else Syntax
if (condition1)
{
    // Code if condition1 is true
}
else if (condition2)
{
    // Code if condition2 is true
}
else
{
    // Code if all conditions are false
}
if-else if-else Example
int score = 85;

if (score >= 90)
{
    Console.WriteLine("Grade: A");
}
else if (score >= 80)
{
    Console.WriteLine("Grade: B");
}
else if (score >= 70)
{
    Console.WriteLine("Grade: C");
}
else if (score >= 60)
{
    Console.WriteLine("Grade: D");
}
else
{
    Console.WriteLine("Grade: F");
}

switch Statement

The switch statement selects one of many code blocks to execute based on an expression's value.

switch Syntax
switch (expression)
{
    case value1:
        // Code for value1
        break;
    case value2:
        // Code for value2
        break;
    default:
        // Code if no cases match
        break;
}
switch Example
string day = "Monday";

switch (day)
{
    case "Monday":
        Console.WriteLine("Start of the work week.");
        break;
    case "Friday":
        Console.WriteLine("Finally, it's Friday!");
        break;
    case "Saturday":
    case "Sunday":
        Console.WriteLine("Weekend!");
        break;
    default:
        Console.WriteLine("Midweek day.");
        break;
}

Switch Expression (C# 8.0+)

C# 8.0 introduced a more concise switch expression syntax:

string result = day switch
{
    "Monday" => "Start of week",
    "Friday" => "Almost weekend",
    "Saturday" or "Sunday" => "Weekend",
    _ => "Weekday"
};

Ternary Operator

The ternary operator is a shorthand for if-else that returns a value based on a condition.

Ternary Operator Syntax
condition ? expression_if_true : expression_if_false;
Ternary Operator Example
int number = 10;
string result = (number % 2 == 0) ? "Even" : "Odd";
Console.WriteLine(result); // Output: Even

Comparison Operators

These operators are used in conditions to compare values:

Operator Description Example
== Equal to x == y
!= Not equal to x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Logical Operators

These operators are used to combine multiple conditions:

Operator Description Example
&& Logical AND x > 5 && x < 10
|| Logical OR x == 5 || x == 10
! Logical NOT !(x > 5)

Best Practices

Keep conditions simple
Complex conditions should be broken down or extracted to well-named variables/methods
Use switch for multiple values
When checking a variable against many constant values, prefer switch over if-else chains
Always include default case
In switch statements, always include a default case to handle unexpected values
Avoid deep nesting
Deeply nested conditionals can be hard to read - consider refactoring
Use parentheses for clarity
Even when not required, parentheses can make complex conditions more readable
Consider polymorphism
For complex conditional logic, consider if polymorphism would be a better solution
Back to Dashboard