C# IF-Else

How if-else works in C#?

If-Else

In C#, the 'if-else' statement is used for conditional branching. It allows you to execute different blocks of code based on whether a specified condition is true or false. Here's the basic syntax of an 'if-else' statement in C#:

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

How it works?

The 'if-else' statement in C# works by evaluating a specified condition. If the condition is 'true', the code block inside the 'if' statement is executed. If the condition is 'false', the code block inside the else statement (if present) is executed. Here's a simple example:

cs Copy Code
using System;

class Program
{
    static void Main()
    {
        int number = 10;

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

Including 'using System;' in examples is a good practice to make them complete and ready to be used without any issues. It helps ensure that beginners understand the basic structure of a C# program and the role of namespaces.

Output:
The number is greater than 5.

* In this example, if the value of the variable number is greater than 5, it will print "The number is greater than 5." to the console; otherwise, it will print "The number is 5 or less."

Multiple If-Else:

You can use multiple if-else statements to handle different conditions in your code. The basic syntax looks like this:

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

Here's an example to illustrate the concept:

cs Copy Code
using System;

class Program
{
    static void Main()
    {
        int number = 10;

        if (number > 0)
        {
            Console.WriteLine("The number is positive.");
        }
        else if (number < 0)
        {
            Console.WriteLine("The number is negative.");
        }
        else
        {
            Console.WriteLine("The number is zero.");
        }
    }
}
Output:
The number is positive.

Nested If-Else:

You can also use nested if-else statements to create conditional branching within your code. Nested if-else statements allow you to check multiple conditions in a hierarchical manner. Here's a simple example:

cs Copy Code
using System;

class Program
{
    static void Main()
    {
        int num = 10;

        if (num > 0)
        {
            Console.WriteLine("The number is positive.");

// Nested if-else
            if (num % 2 == 0)
            {
                Console.WriteLine("The number is even.");
            }
            else
            {
                Console.WriteLine("The number is odd.");
            }
        }
        else if (num < 0)
        {
            Console.WriteLine("The number is negative.");
        }
        else
        {
            Console.WriteLine("The number is zero.");
        }
    }
}
Output:
The number is positive.
The number is even.

Explanation:

The outer 'if' statement checks if 'num' is greater than 0.

If it is, it enters the block and checks whether the number is even or odd using a nested 'if-else statement.

If 'num' is less than 0, the 'else if' block is executed, printing that the number is negative.

If neither condition is true, the 'else' block is executed, indicating that the number is zero.

Note: You can nest if-else statements as needed based on the complexity of your logic. Just ensure that you maintain proper indentation and structure for better code readability.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.