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.
Urgenthomework helped me with finance homework problems and taught math portion of my course as well. Initially, I used a tutor that taught me math course I felt that as if I was not getting the help I needed. With the help of Urgenthomework, I got precisely where I was weak:
Read More
Follow Us