Nested if Statement in C# Programming Language

Rumman Ansari   Software Engineer   2019-03-17   5579 Share
☰ Table of Contents

Table of Content:


Nested if Statements

C# alows nested if else statements. The nested 'if' statement makes the code more readable.

Example: Nested if statements


int i = 10;

if (i > 0)
{
    if (i <= 100)
    {
        Console.WriteLine("i is positive number less than 100");
    }
    else 
    {
        Console.WriteLine("i is positive number greater than 100");
    }
}

Output


  i is positive number less than 100

The if-else statement can be replaced by ternary operator. Learn about ternary operator in the next section.

Points to Remember :

  1. if-else statement controls the flow of program based on the evaluation of the boolean expression.
  2. It should start from the if statement followed by else or else-if statements.
  3. Only one else statement is allowed in the if-else chain.
  4. Multiple else-if statements are allowed in a single if-else chain.
  5. Nested if-else statement is allowed.