Easy Tutorial
❮ Csharp Jagged Arrays Csharp Type Conversion ❯

C# if...else Statement

C# Decision Making

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax

The syntax for an if...else statement in C# is as follows:

if(boolean_expression)
{
   /* Statements to execute if the boolean expression is true */
}
else
{
  /* Statements to execute if the boolean expression is false */
}

If the boolean expression is true, the code inside the if block is executed. If the boolean expression is false, the code inside the else block is executed.

Flowchart

Example

Example

using System;

namespace DecisionMaking
{
    class Program
    {
        static void Main(string[] args)
        {
            /* Local variable declaration */
            int a = 100;

            /* Check the boolean condition */
            if (a < 20)
            {
                /* If the condition is true, print the following statement */
                Console.WriteLine("a is less than 20");
            }
            else
            {
                /* If the condition is false, print the following statement */
                Console.WriteLine("a is greater than 20");
            }
            Console.WriteLine("The value of a is {0}", a);
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces the following result:

a is greater than 20
The value of a is 100

if...else if...else Statement

An if statement can be followed by an optional else if...else statement, which is useful for testing multiple conditions.

When using if...else if...else statements, the following points should be noted:

Syntax

The syntax for an if...else if...else statement in C# is as follows:

if(boolean_expression 1)
{
   /* Executed when the boolean expression 1 is true */
}
else if(boolean_expression 2)
{
   /* Executed when the boolean expression 2 is true */
}
else if(boolean_expression 3)
{
   /* Executed when the boolean expression 3 is true */
}
else 
{
   /* Executed when none of the above conditions are true */
}

Example

Example

using System;

namespace DecisionMaking
{
    class Program
    {
        static void Main(string[] args)
        {
            /* Local variable declaration */
            int a = 100;

            /* Check the boolean condition */
            if (a == 10)
            {
                /* If the if condition is true, print the following statement */
                Console.WriteLine("The value of a is 10");
            }
            else if (a == 20)
            {
                /* If the else if condition is true, print the following statement */
                Console.WriteLine("The value of a is 20");
            }
            else if (a == 30)
            {
                /* If the else if condition is true, print the following statement */
                Console.WriteLine("The value of a is 30");
            }
            else
            {
                /* If none of the above conditions are true, print the following statement */
                Console.WriteLine("None of the values is matching");
            }
            Console.WriteLine("The exact value of a is: {0}", a);
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces the following result:

None of the values is matching
The exact value of a is: 100
{
    /* If none of the above conditions are true, output the following statement */
    Console.WriteLine("No matching value");
}
Console.WriteLine("The exact value of a is {0}", a);
Console.ReadLine();
}
}

When the above code is compiled and executed, it produces the following result:

No matching value
The exact value of a is 100

C# Decision

❮ Csharp Jagged Arrays Csharp Type Conversion ❯