C# if-else Statement

Introduction

The if-else statement is a fundamental control flow structure that allows you to execute different blocks of code based on certain conditions. In C#, there are several forms of the if-else statement:

  1. if Statement
  2. if-else Statement
  3. Nested if Statement
  4. if-else-if Ladder

1. if Statement

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

Syntax

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

Example

using System;

namespace IfStatementExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 10;

            // Check if number is greater than 5
            if (number > 5)
            {
                Console.WriteLine("The number is greater than 5.");
            }

            // This will always be executed
            Console.WriteLine("Program finished.");
        }
    }
}

Output:

The number is greater than 5.
Program finished.

2. if-else Statement

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

Syntax

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

Example

using System;

namespace IfElseStatementExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 3;

            // Check if number is greater than 5
            if (number > 5)
            {
                Console.WriteLine("The number is greater than 5.");
            }
            else
            {
                Console.WriteLine("The number is 5 or less.");
            }

            // This will always be executed
            Console.WriteLine("Program finished.");
        }
    }
}

Output:

The number is 5 or less.
Program finished.

3. Nested if Statement

A nested if statement is an if statement inside another if statement. This allows for checking multiple conditions.

Syntax

if (condition1)
{
    // Code to execute if condition1 is true
    if (condition2)
    {
        // Code to execute if condition2 is also true
    }
}

Example

using System;

namespace NestedIfStatementExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 10;

            // Check if number is greater than 5
            if (number > 5)
            {
                Console.WriteLine("The number is greater than 5.");

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

            // This will always be executed
            Console.WriteLine("Program finished.");
        }
    }
}

Output:

The number is greater than 5.
The number is even.
Program finished.

4. if-else-if Ladder

The if-else-if ladder allows you to test multiple conditions sequentially. The first true condition’s block of code will be executed.

Syntax

if (condition1)
{
    // Code to execute if condition1 is true
}
else if (condition2)
{
    // Code to execute if condition2 is true
}
else if (condition3)
{
    // Code to execute if condition3 is true
}
else
{
    // Code to execute if none of the above conditions are true
}

Example

using System;

namespace IfElseIfLadderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 15;

            // Check multiple conditions using if-else-if ladder
            if (number < 0)
            {
                Console.WriteLine("The number is negative.");
            }
            else if (number == 0)
            {
                Console.WriteLine("The number is zero.");
            }
            else if (number > 0 && number <= 10)
            {
                Console.WriteLine("The number is between 1 and 10.");
            }
            else
            {
                Console.WriteLine("The number is greater than 10.");
            }

            // This will always be executed
            Console.WriteLine("Program finished.");
        }
    }
}

Output:

The number is greater than 10.
Program finished.

Conclusion

The if-else statement in C# is used for controlling the flow of a program based on conditions. By using if statements, if-else statements, nested if statements, and if-else-if ladders, you can handle complex decision-making in your programs. Understanding these constructs is essential for writing flexible and dynamic code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top