Nested If Else Statement in C Language

In Nested If Else Statement in C, an entire if…else construct is written within either the body of the if statement or the body of an else statement.

The syntax of Nested If Else Statement in C Language

if (condition_p)
{
if (condition_q)
{
Statements_1_Block;
}
else
{
Statements_2_Block;
}
}
else
{
Statements_3_Block;
}
Statement_4_Block;

Here, condition_p is evaluated. If it is false then Statements_3_Block is executed and is followed by the execution of Statements_4_Block, otherwise, if condition_p is true, then condition_q is evaluated.

Statements_1_Block is executed when condition_q is true otherwise Statements_2_Block is executed and then the control is transferred to Statements_4_Block.

Nested If Else statement in c
Nested If Else

A program to illustrate Nested if else statement in C

Write a program to calculate an Air ticket fare after discount, given the following conditions:If

  • If passenger is below 14 years then there is 50% discount on fare
  • If passenger is above 50 years then there is 20% discount on fare
  • If passenger is above 14 and below 50 then there is 10% discount on fare.

 

/* Program to calculate an Air ticket fare after discount */

#include <stdio.h>

main( )

{

int age;

float fare;

printf(“\n Enter the age of passenger:\n”);

scanf(“%d”,&age);

printf(“\n Enter the Air ticket fare\n”);

scanf(“%f”,&fare);

if (age < 14)

fare = fare – 0.5 * fare;

else

if (age <= 50)

{

fare = fare – 0.1 * fare;

}

else

{

fare = fare – 0.2 * fare;

}

printf(“\n Air ticket fare to be charged after discount is %.2f”,fare);

}

OUTPUT 

Enter the age of passenger
12
Enter the Air ticket fare
2000.00
Air ticket fare to be charged after discount is 1000.00

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: