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
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.
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 */
printf(“\n Enter the age of passenger:\n”);
printf(“\n Enter the Air ticket fare\n”);
if (age < 14)
fare = fare – 0.5 * fare;
if (age <= 50)
fare = fare – 0.1 * fare;
fare = fare – 0.2 * fare;
printf(“\n Air ticket fare to be charged after discount is %.2f”,fare);
Enter the age of passenger
Enter the Air ticket fare
Air ticket fare to be charged after discount is 1000.00