if Statements in C# programming

if Statement

if statement is conditional or decision making statement. It uses boolean variable or a condition which gives a boolean result and a statement or block of statements which will be executed only if the condition is satisfied or boolean result is true.

Syntax for if Statement:

if (condition or boolean variable)
{
     //statements
}

Flowchart of if Statement:

Example 1: C# Program of if Statement

C# Program to check if a number is greater than other.

using System;
namespace Conditional
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2, b = 1;
            if (a > b)
                Console.WriteLine("a is greater than b!");
            Console.ReadLine();
        }
    }
}

In above program, if statement checks if variable a is greater than variable b or not. If a is greater that b, the program displays a is greater than!.

Output:

a is greater than b!

if else Statement

if else Statement is very similar to if statement but there is additional else statement where block of expressions that need to be executed if it doesn’t satisfy if condition is present. Using else statement in if Statement is not compulsory. An if Statement can only have one else statement.

Syntax for if else Statement:

if (condition or boolean variable)
{
     //statements
}
else
{
     //statements
}

Flowchart for if else Statement:

Example 2: C# Program to demonstrate use of if else Statement

Program to find greater number among two numbers.

using System;
namespace Conditional
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2, b = 3;
            if (a > b)
                Console.WriteLine("a is greater than b!");
            else
                Console.WriteLine("b is greater than a!");
            Console.ReadLine();
        }
    }
}

In above program, if statement checks if a is greater than b or not, if yes the program displays message “a is greater than b!” and doesn’t execute the else block. But, if the condition is not satisfied it executes the else block and displays the message “b is greater than a!”.

Output:

b is greater than a!

if else if …. else Statement

if else if .. else Statement is used where there are more than two conditions that need to be checked to decide which statements are to be executed/ Using else if statement in if Statement is not compulsory. An if Statement can have multiple number of else if statements as required.

Syntax for if else if .. else Statement:

if (condition or boolean variable)
{
     //statements
}
else if (second condition or boolean variable)
{
     //statements
}
.
.
.
else
{
     //statements
}

Flowchart for if else if … else Statement:

Example 3: C# Program of if else if … else Statement:

Program to find greatest number among three numbers.

using System;
namespace Conditional
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2, b = 1, c = 5;
            if (a > b && a > c)
                Console.WriteLine("a is the greatest number!");
            else if (b > a && b > c)
                Console.WriteLine("b is the greatest number!");
            else
                Console.WriteLine("c is the greatest number!");
            Console.ReadLine();
        }
    }
}

In above program, if statement checks if variable a is greater than both variable b and c or not. If yes, it displays the message “a is the greatest number!”. If the condition is not satisfied, it moves to else if statement and checks if b is greater than both a and c or not. If yes, the program displays the message “b is the greatest number!” if not it moves to else statement and display the message “c is the greatest number!”

Output:

c is the greatest number!

Nested if Statement

Nested if statement is one if Statement or else Statement inside another if Statement or else Statement.

Syntax for Nested if Statement:

if (condition or boolean variable)
{
    //statements
    if (second condition or boolean variable)
    {
        //statements
    }
}

Flowchart for Nested if Statement:

Example 4: C# Program of Nested if Statement

Program to find greatest number among three numbers using Nested if Statement.

using System;
namespace Conditional
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2, b = 1, c = 5;
            if (a > b)
            {
                if (a > c)
                    Console.WriteLine("a is the greatest number!");
                else
                    Console.WriteLine("c is the greatest number!");
            }
            else
            {
                if (b > a)
                    Console.WriteLine("b is the greatest number!");
                else
                    Console.WriteLine("c is the greatest number!");
            }
            Console.ReadLine();
        }
    }
}

In above program, first the if statement checks if a is greater than b or not, if yes it moves to the inner conditional statement and checks if a is greater than c or not. If yes it displays the message “a is the greatest number!”, if not it displays the message “c is the greatest number!”. And if the first statement was not satisfied the outer else statement is checked and the same process is repeated again.

Output:

c is the greatest number!

Additional Information about if Statement for using it correctly

  • In programming languages like C and C++, boolean expression cannot be in integer format.
  • Block of statements to be executed if the boolean result is true must be enclosed within curly braces ‘{ }’ if they extend more than one line
  • If we have only one statement in the body of if statement, it is not necessary to enclose that statement with curly braces. But, if we don’t use curly braces for multiple statements, only the first statement will be conditional statement and rest of the statements will be like normal statements and will be executed no matter what the boolean result is.
            if (1) //invalid
            {
                Console.WriteLine("It is true!");
            }

            if (true)
            {
                Console.WriteLine("It is true!"); //will be executed
            } //valid

            if (true)
                Console.WriteLine("It is true!"); //valid and will be executed

            if (false)
                Console.WriteLine("This line will not be executed!");
                Console.WriteLine("This line would not have be executed if it was in between curly braces!"); //will be executed always