Nested if-else statement
When If – Else is used within If-ElseStatement it is called as Nested If-Else Statement.
Syntax:
if (condition 1)
{
{//}Statement to be executed when condition 1 true;
}
else
{
if (condition 2)
{
Statement to be executed when condition 2 is true;
}
else
{
Statement to be executed when condition 2 is false;
}
}
Let us re-program the Indian railways ticket program using Nested If-Else Statement.
#include <stdio.h>
#include <conio.h>
int main()
{
int age;
printf(“Enter your age: ”);
scanf(“%d”,&age);
if (age <= 5)
{
printf(“you have 100% discount on your ticket.”);
}
else
{
if (age <= 12)
{
printf(“you have 50% discount on your ticket.”);
}
else
{
if (age <=26)
{
printf(“you have 10% discount on your ticket.”);
}
else
{
if (age > 60)
{
printf(“you have 25% discount on your ticket.”);
}
}
}
}
return;
}
In the above example we have implemented Nested If-Else Statement.
- If Statement
- Multiple if statement
- If else statement
- Nested if else statement
- Else if statement
- Conditional Operator
- The switch statement