- Back to Home »
- C#.NET »
- C#.NET Nested If
Posted by :
Sudhir Chekuri
Thursday, 31 December 2015
It is one type of If statement. Nested If means if in if ie.,The if statement contains another if within that.
Syntax for Nested If
if(condition)
{
if(condition)
{
if(condition)
{
statements
}
}
}
Example program for Nested If Statements
Program 27
Syntax for Nested If
if(condition)
{
if(condition)
{
if(condition)
{
statements
}
}
}
Example program for Nested If Statements
Program 27
using System;
namespace ifelse
{
class Program
{
static void Main(string[] args)
{
int i;
Console.WriteLine("Enter number");
i = Convert.ToInt32(Console.ReadLine());
if (i > 0)
{
if (i > 5 && i < 10)
{
Console.WriteLine("Between 5 and 10");
}
}
Console.ReadKey();
}
}
}
Output
Enter number
4
_
Output
Enter number
8
Between 5 and 10
Explanation
In the above example we can give input is 4,nothing will be printed in output because 4 is greater than zero but it is not in between 5 and 10.so,the output will be _.In second output we can give input is 8,it is in between 5 and 10 so,it will be printed as Between 5 and 10.