How To Use C# If Else Statement?

Introduction

C# language supports most of the modern common language control statements, including the if else statement. The if else statement in C# is one of the most commonly used selection statements for if conditions. The code example in this article shows how to use an if-else statement in C#.

C# if else statement checks a Boolean expression. The Boolean expression returns a value, true or false. The if part of the code executes when the value of the expression is true. The else part of the code executes when the value of the expression is false.

CSharp If Else Statement

Here is the syntax of the if else statement.

if (boolean condition)
{
    // Statement
}
else
{
    // Statement
}

The if section of the statement or code block is executed when the condition is true; if its false, control executes the code written in the else statement or block. The ‘else’ portion of the statement is optional.

The following code example uses an if statement to check if the value of a variable a is less than 0, then displays a message, ‘a is negative’.

int a = -1;
if (a < 0)
{
    Console.WriteLine("a is negative.");
}

The following code snippet uses an if else statement to check if the value of a is less than 0. If not, then display a message, ‘a is 0 or positive’.

int a = -1;
if (a < 0)
{
    Console.WriteLine("a is negative.");
}
else
{
    Console.WriteLine("a is 0 or positive.");
}

The {} brackets are optional for a single-line statement. We can replace the above code with the following code.

if (a < 0)
    Console.WriteLine("a is negative.");
else
    Console.WriteLine("a is 0 or positive.");

The if else if

We can also use if with else if to add one more if condition in the statement. You can use as many as else or else if with one if.

The following code example uses the if else if statement to check for two if conditions. If both the if conditions are false, then the else code block is executed.

int a = -1;
if (a < 0)
{
    Console.WriteLine("a is negative.");
}
else if (a == 0)
{
    Console.WriteLine("a is 0.");
}
else
{
    Console.WriteLine("a is positive.");
}

Conditional operators in if else

You can also apply conditional or ( || ) and conditional and (&&) operators to combine more than one condition. Listing 56 shows you how to use the if-else statement.

int a = -1;
int b = 10;
int c;
// Check if both numbers are negative
if (a < 0 && b < 0)
{
    Console.WriteLine("Both a and b are negative.");
}
else if (a < 0 || b < 0)
{
    Console.WriteLine("One number is negative.");
}
else
{
    Console.WriteLine("Both numbers are positive.");
}

Nested if else in C#

You can have a nested if else if statement with one or more else blocks. Nested if can be used in both if and else both parts of the statement. The following code sample implements nested if else if statement.

if (a < 0 && b < 0)
{
    Console.WriteLine("Both a and b are negative.");
}
else if (a < 0 || b < 0)
{
    if (b > 0 && b <= 10)
    {
        c = a + b;
        Console.WriteLine("Total: {0}", c);
    }
}

The if else if Code Example in C#

Here is a complete code example that uses several if, else if statements, including nested statements.

using System;
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Statements Sample!");

        int a = -1;
        int b = 10;
        int c;

        // Check if both numbers are negative
        if (a < 0 && b < 0)
        {
            Console.WriteLine("Both a and b are negative.");
        }
        else if (a < 0 || b < 0)
        {
            if (b > 0 && b <= 10)
            {
                c = a + b;
                Console.WriteLine("Total: {0}", c);
            }
            Console.WriteLine("One number is negative.");
        }
        else
        {
            Console.WriteLine("Both numbers are positive.");
        }
        Console.ReadKey();
    }
}

C# supports two selection statements, the if..else and the switch case.

Learn more here about Switch Statement in C#


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.