Definition:
The else if statement is used when there are multiple conditions to check. It evaluates each condition one by one, and the first condition that is true is executed. If none of the conditions are true, the else block is executed.
Syntax:
if(condition1)
{
// statements
}
else if(condition2)
{
// statements
}
else if(condition3)
{
// statements
}
else
{
// statements
}
Explanation:
This program accepts the purchase amount from the user. Based on the entered amount, it checks different ranges using the else if statement and assigns a discount percentage.
The program then calculates the discount amount and displays the final amount after deducting the discount.
Explanation: This program accepts the purchase amount from the user, determines the discount percentage using the else if statement, calculates the discount amount, and displays the final payable amount.
#include <stdio.h> #include <conio.h> main() { float b,d,a,c; clrscr(); printf("\nEnter Amount : "); scanf("%f",&b); if(b >= 300 && b <= 1000) { d = 20; } else if(b >= 1001 && b <= 1500) { d = 30; } else if(b >= 1501 && b < 2000) { d = 40; } else { d = 0; } printf("\nDiscount Percentage = %.2f",d); c = b * d / 100; a = b - c; printf("\nDiscount Amount = %.2f",c); printf("\nAmount After Discount = %.2f",a); getch(); }
else if statement to check multiple salary ranges and calculate the bonus amount.Bonus = Salary × Bonus Percentage / 100.
#include <stdio.h> #include <conio.h> main() { float salary, bonus, amount; clrscr(); printf("Enter Salary : "); scanf("%f",&salary); if(salary >= 20000 && salary <= 30000) { bonus = 10; } else if(salary > 30000 && salary <= 50000) { bonus = 15; } else if(salary > 50000) { bonus = 20; } else { bonus = 0; } amount = salary * bonus / 100; printf("\nBonus = %.2f",amount); getch(); }
else if statement, calculates the bonus percentage, and displays the bonus amount.