Easy Learn C#

C# If...Else Statements

Introduction to Conditional Statements

Conditional statements allow your programs to make decisions based on certain conditions. They enable your code to execute different blocks of statements depending on whether a specified condition evaluates to true or false.

Key Conditional Statement Concepts:

  • Conditional statements control the flow of program execution
  • All conditions in C# must evaluate to a boolean (true or false)
  • Conditions can use comparison operators (==, !=, >, <, >=, <=)
  • Conditions can use logical operators (&&, ||, !)
  • Conditional statements can be nested inside each other

Basic If Statement

The most basic form of a conditional statement is the if statement. It executes a block of code only if the specified condition evaluates to true.

Basic If Statement Syntax:


if (condition)
{
    // Code to execute if condition is true
}
                            

When using a single statement after an if, braces are optional, but it's good practice to always include them:


// With single statement, braces are optional
if (temperature > 30)
    Console.WriteLine("It's hot outside!");

// Better practice (always use braces)
if (temperature > 30)
{
    Console.WriteLine("It's hot outside!");
}
                            

If Statement Examples:


// Simple if statement
int age = 20;
if (age >= 18)
{
    Console.WriteLine("You are an adult.");
}

// If statement with multiple conditions using logical AND
bool hasLicense = true;
if (age >= 18 && hasLicense)
{
    Console.WriteLine("You can legally drive.");
}

// If statement with logical OR
bool isHoliday = false;
bool isWeekend = true;
if (isHoliday || isWeekend)
{
    Console.WriteLine("You don't have to go to work.");
}

// If statement with logical NOT
bool isWorkDay = true;
if (!isWorkDay)
{
    Console.WriteLine("You can relax today.");
}
                            

If-Else Statement

The if-else statement executes one block of code if a condition is true and another block if the condition is false.

If-Else Syntax:


if (condition)
{
    // Code to execute if condition is true
}
else
{
    // Code to execute if condition is false
}
                            

If-Else Examples:


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

// If-else with variable assignment
bool isEligible;
if (age >= 18)
{
    isEligible = true;
}
else
{
    isEligible = false;
}

// The above can be simplified using a ternary operator:
isEligible = (age >= 18) ? true : false;
// Even simpler:
isEligible = age >= 18;

// If-else with complex conditions
double balance = 5000;
bool isPremium = false;
if (balance > 10000 || isPremium)
{
    Console.WriteLine("No service fee will be charged.");
}
else
{
    Console.WriteLine("A service fee will be applied.");
}
                            

If-Else-If Ladder

An if-else-if ladder allows you to check multiple conditions in sequence, executing the block associated with the first true condition.

If-Else-If Syntax:


if (condition1)
{
    // Code to execute if condition1 is true
}
else if (condition2)
{
    // Code to execute if condition1 is false and condition2 is true
}
else if (condition3)
{
    // Code to execute if condition1 and condition2 are false, and condition3 is true
}
else
{
    // Code to execute if all conditions are false
}
                            

If-Else-If Examples:


// Grading system
int score = 85;
string grade;

if (score >= 90)
{
    grade = "A";
}
else if (score >= 80)
{
    grade = "B";
}
else if (score >= 70)
{
    grade = "C";
}
else if (score >= 60)
{
    grade = "D";
}
else
{
    grade = "F";
}

Console.WriteLine($"Your grade is: {grade}");

// Time of day greeting
int hour = 14; // 24-hour format
string greeting;

if (hour < 12)
{
    greeting = "Good morning!";
}
else if (hour < 18)
{
    greeting = "Good afternoon!";
}
else
{
    greeting = "Good evening!";
}

Console.WriteLine(greeting);
                            

Important notes about if-else-if ladders:

  • Conditions are evaluated in order (top to bottom)
  • Once a condition evaluates to true, its associated code block is executed
  • After that block executes, the program jumps past the entire if-else-if ladder
  • The final else block is optional and acts as a catch-all for when no conditions are true

Nested If Statements

You can place if statements inside other if or else blocks to create nested conditions. This allows you to check for conditions within conditions.

Nested If Statement Examples:


// Authentication and authorization check
bool isAuthenticated = true;
bool isAdmin = false;
bool isAuthor = true;

if (isAuthenticated)
{
    Console.WriteLine("User is authenticated.");
    
    if (isAdmin)
    {
        Console.WriteLine("Admin access granted.");
    }
    else
    {
        Console.WriteLine("Admin access denied.");
        
        if (isAuthor)
        {
            Console.WriteLine("Author access granted.");
        }
        else
        {
            Console.WriteLine("Regular user access granted.");
        }
    }
}
else
{
    Console.WriteLine("User is not authenticated. Access denied.");
}

// Loan eligibility check
int creditScore = 720;
double income = 60000;
double loanAmount = 200000;

if (creditScore >= 700)
{
    if (income >= 50000)
    {
        if (loanAmount <= income * 5)
        {
            Console.WriteLine("Loan approved!");
        }
        else
        {
            Console.WriteLine("Loan denied. Amount too high relative to income.");
        }
    }
    else
    {
        Console.WriteLine("Loan denied. Income too low.");
    }
}
else
{
    Console.WriteLine("Loan denied. Credit score too low.");
}
                            

While nested if statements provide fine control over complex conditions, they can quickly become hard to read and maintain. Consider restructuring or using logical operators to simplify deeply nested conditional code.

Simplifying Nested Ifs with Logical Operators:


// Instead of this nested approach:
if (isAuthenticated)
{
    if (isAdmin)
    {
        Console.WriteLine("Admin access granted.");
    }
}

// You can use logical AND:
if (isAuthenticated && isAdmin)
{
    Console.WriteLine("Admin access granted.");
}

// Similarly, the loan example could be rewritten:
if (creditScore >= 700 && income >= 50000 && loanAmount <= income * 5)
{
    Console.WriteLine("Loan approved!");
}
else
{
    Console.WriteLine("Loan denied. Please review eligibility criteria.");
}
                            

Conditional Operator (Ternary Operator)

The conditional operator ?: provides a compact way to write simple if-else statements, especially useful for variable assignments.

Ternary Operator Syntax:


// Syntax: condition ? expression_if_true : expression_if_false
result = condition ? valueIfTrue : valueIfFalse;
                            

Ternary Operator Examples:


// Simple ternary expression
int age = 20;
string status = (age >= 18) ? "Adult" : "Minor";

// Ternary in string interpolation
Console.WriteLine($"The person is {((age >= 18) ? "an adult" : "a minor")}.");

// Chained ternary operators (use sparingly as they can reduce readability)
int score = 85;
string grade = (score >= 90) ? "A" : 
               (score >= 80) ? "B" : 
               (score >= 70) ? "C" : 
               (score >= 60) ? "D" : "F";

// Ternary with method calls
bool hasPermission = true;
void GrantAccess() { /* ... */ }
void DenyAccess() { /* ... */ }

// Call different methods based on condition
(hasPermission ? GrantAccess : DenyAccess)();
                            

Guidelines for using the ternary operator:

  • Use it for simple, straightforward conditions
  • Avoid nesting multiple ternary operators as it reduces readability
  • Both expressions must be of compatible types
  • It's particularly useful for quick assignments or method arguments

Null-Conditional Operator (?.) and Null-Coalescing Operator (??)

C# provides special operators for handling null values in conditional contexts.

Null-Conditional and Null-Coalescing Examples:


// Null-conditional operator (?.) - safely access members of potentially null objects
string name = null;
int? length = name?.Length;  // Returns null instead of throwing NullReferenceException

// Null-coalescing operator (??) - provides a default value if the expression is null
string username = name ?? "Guest";  // "Guest"

// Combining them
string text = null;
int textLength = text?.Length ?? 0;  // 0 (safe access and default if null)

// Null-coalescing assignment operator (??=) (C# 8.0+)
string message = null;
message ??= "Default message";  // Assigns only if message is null
Console.WriteLine(message);     // "Default message"
                            

These operators help write more concise and robust code that gracefully handles null values without explicit if-else statements.

Best Practices for Conditional Statements

Following these practices will make your conditional code more maintainable and less prone to bugs:

Proper Formatting:


// DO: Use consistent indentation and braces
if (condition)
{
    // Code block
}
else
{
    // Code block
}

// DON'T: Inconsistent or missing braces
if (condition)
    Console.WriteLine("True");
else {
    Console.WriteLine("False");
    DoMoreWork();
}
                            

Simplify Complex Conditions:


// Instead of:
if (age >= 18 && age <= 65 && !hasDisability && isEmployed && yearOfService > 2)
{
    // Eligible for standard benefits
}

// DO: Extract complex conditions into well-named methods or variables
bool IsEligibleForStandardBenefits()
{
    return IsWorkingAge() && isEmployed && HasSufficientServiceYears();
}

bool IsWorkingAge()
{
    return age >= 18 && age <= 65 && !hasDisability;
}

bool HasSufficientServiceYears()
{
    return yearOfService > 2;
}

if (IsEligibleForStandardBenefits())
{
    // Eligible for standard benefits
}
                            

Guard Clauses for Early Returns:


// Instead of deeply nested if-else blocks:
void ProcessOrder(Order order)
{
    if (order != null)
    {
        if (order.IsValid)
        {
            if (order.Items.Count > 0)
            {
                if (order.PaymentCompleted)
                {
                    // Process the order
                    // ...
                }
                else
                {
                    throw new Exception("Payment not completed");
                }
            }
            else
            {
                throw new Exception("Order has no items");
            }
        }
        else
        {
            throw new Exception("Order is invalid");
        }
    }
    else
    {
        throw new Exception("Order is null");
    }
}

// DO: Use guard clauses for early returns:
void ProcessOrder(Order order)
{
    // Handle invalid cases first
    if (order == null)
        throw new ArgumentNullException(nameof(order));
        
    if (!order.IsValid)
        throw new Exception("Order is invalid");
        
    if (order.Items.Count == 0)
        throw new Exception("Order has no items");
        
    if (!order.PaymentCompleted)
        throw new Exception("Payment not completed");
    
    // Now process the valid order
    // ...
}
                            

Guard clauses:

  • Validate preconditions at the beginning of methods
  • Reduce nesting and improve readability
  • Make error conditions more explicit
  • Allow the main code to focus on the "happy path"

Avoid Common Pitfalls:


// DON'T: Assignment instead of comparison
if (x = 10)  // Assigns 10 to x, then evaluates to true because 10 is non-zero
{
    // This will always execute!
}

// DO: Use comparison operators for conditions
if (x == 10)  // Compares x to 10
{
    // Executes only if x equals 10
}

// DON'T: Always check the most specific condition first
if (isUser)
{
    // User-specific code
}
else if (isUser && isAdmin)  // This will never execute!
{
    // Admin-specific code
}

// DO: Check the most specific condition first
if (isUser && isAdmin)
{
    // Admin-specific code
}
else if (isUser)
{
    // Regular user-specific code
}