C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# If…else Statement

In this tutorial, we will explore the if...else statement in C#. The if...else statement is a fundamental control flow statement in programming that allows you to conditionally execute code based on a boolean expression.

  • Basic Syntax

The basic syntax of the if...else statement is as follows:

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

The condition is a boolean expression that evaluates to either true or false. If the condition is true, the code block following the if statement is executed. If the condition is false, the code block following the else statement is executed.

  • Using If...else

Here's an example of using the if...else statement to determine if a number is even or odd:

int number = 7;

if (number % 2 == 0)
{
    Console.WriteLine("The number is even.");
}
else
{
    Console.WriteLine("The number is odd.");
}

In this example, the condition checks if the remainder of number divided by 2 is 0. If the condition is true, the number is even; otherwise, the number is odd.

  • If...else if...else

You can use the else if statement to chain multiple conditions together:

int grade = 85;

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

In this example, the code checks the grade variable against multiple conditions to determine the corresponding letter grade. The first true condition encountered will execute its associated code block, and the remaining conditions will be skipped.

  • Nested If...else

You can nest if...else statements inside each other to create more complex conditions:

int number = 25;

if (number > 0)
{
    if (number % 2 == 0)
    {
        Console.WriteLine("The number is positive and even.");
    }
    else
    {
        Console.WriteLine("The number is positive and odd.");
    }
}
else
{
    Console.WriteLine("The number is not positive.");
}

In this example, we first check if the number is positive. If it is, we then check if it's even or odd using a nested if...else statement.

This tutorial demonstrates the basics of the if...else statement in C#. The if...else statement allows you to conditionally execute code based on the result of a boolean expression. You can chain multiple conditions using the else if statement or nest if...else statements inside each other to create more complex conditions.

  1. How to use if...else in C#

    The if...else statement is a fundamental part of C# for making decisions in code. Here's a basic example:

    int number = 10;
    
    if (number > 0)
    {
        Console.WriteLine("Number is positive");
    }
    else
    {
        Console.WriteLine("Number is non-positive");
    }
    
  2. Using if...else with boolean expressions in C#

    The condition inside the if statement is a boolean expression. Here's an example:

    bool isSunny = true;
    
    if (isSunny)
    {
        Console.WriteLine("Enjoy the sunshine!");
    }
    else
    {
        Console.WriteLine("It's not sunny today.");
    }
    
  3. Nested if...else statements in C#

    You can nest if...else statements to handle multiple conditions. Example:

    int x = 10;
    int y = 5;
    
    if (x > 0)
    {
        if (y > 0)
        {
            Console.WriteLine("Both x and y are positive");
        }
        else
        {
            Console.WriteLine("x is positive, but y is non-positive");
        }
    }
    else
    {
        Console.WriteLine("x is non-positive");
    }
    
  4. Ternary operator vs if...else in C#

    The ternary operator (? :) is a concise way to write simple if...else statements. Example:

    int age = 20;
    string result = (age >= 18) ? "Adult" : "Minor";
    
  5. C# if...else if...else ladder

    An if...else if...else ladder allows checking multiple conditions in sequence:

    int score = 75;
    
    if (score >= 90)
    {
        Console.WriteLine("Grade A");
    }
    else if (score >= 80)
    {
        Console.WriteLine("Grade B");
    }
    else if (score >= 70)
    {
        Console.WriteLine("Grade C");
    }
    else
    {
        Console.WriteLine("Grade F");
    }
    
  6. Switch statement vs if...else in C#

    The switch statement is an alternative to if...else for handling multiple cases. Example:

    int dayOfWeek = 3;
    
    switch (dayOfWeek)
    {
        case 1:
            Console.WriteLine("Monday");
            break;
        case 2:
            Console.WriteLine("Tuesday");
            break;
        // Handle other cases
        default:
            Console.WriteLine("Invalid day");
            break;
    }
    
  7. Logical operators and if...else in C#

    Logical operators (&&, ||, !) can be used in if statements to create more complex conditions:

    int temperature = 25;
    bool isSunny = true;
    
    if (temperature > 20 && isSunny)
    {
        Console.WriteLine("It's a warm and sunny day!");
    }
    
  8. Using if...else with null checks in C#

    You can use if statements for null checks to prevent null reference exceptions:

    string name = null;
    
    if (name != null)
    {
        Console.WriteLine($"Name: {name}");
    }
    else
    {
        Console.WriteLine("Name is null");
    }
    
  9. Short-circuiting in if...else statements in C#

    Short-circuiting occurs when the second part of a logical expression is not evaluated if the first part determines the outcome. Example:

    int x = 5;
    int y = 0;
    
    if (y != 0 && x / y > 2)
    {
        Console.WriteLine("This won't be executed due to short-circuiting");
    }