Definition:
The else if else statement is used to test multiple conditions one after another. It is useful when a program needs to make decisions based on different ranges of values. In this program, it is used to determine whether a student has passed or failed and to assign the appropriate grade based on the average marks.
Syntax:
if(condition)
{
// statements
}
else if(condition)
{
// statements
}
else if(condition)
{
// statements
}
else
{
// statements
}
Explanation:
This program accepts the marks of four subjects: Urdu, English, Maths, and Science. It calculates the total marks and average marks. First, it checks whether the student has passed in all subjects by ensuring each subject has at least 35 marks. Then, using the else if statement, it assigns a grade according to the average marks.
Explanation: This program accepts marks of four subjects, calculates the total and average marks, checks whether the student has passed or failed, and displays the appropriate grade using the if and else if statements.
#include <stdio.h> #include <conio.h> main() { int urd,eng,math,sci,total,avg; clrscr(); printf("90 to 100 A++"); printf("\n75 to 89 A+"); printf("\n60 to 74 A"); printf("\n45 to 59 B"); printf("\n35 to 44 C"); printf("\n\nUrdu : "); scanf("%d",&urd); printf("\nEnglish : "); scanf("%d",&eng); printf("\nMaths : "); scanf("%d",&math); printf("\nScience : "); scanf("%d",&sci); total = urd + eng + math + sci; avg = total / 4; printf("\nTotal Marks : %d",total); printf("\nAverage : %d",avg); if(urd >= 35 && eng >= 35 && math >= 35 && sci >= 35) { printf("\nResult : Pass"); } else { printf("\nResult : Fail"); } if(avg >= 90 && avg <= 100) { printf("\nGrade : A++"); } else if(avg >= 75 && avg <= 89) { printf("\nGrade : A+"); } else if(avg >= 60 && avg <= 74) { printf("\nGrade : A"); } else if(avg >= 45 && avg <= 59) { printf("\nGrade : B"); } else if(avg >= 35 && avg <= 44) { printf("\nGrade : C"); } else { printf("\nResult : Fail"); } getch(); }
else if statement to assign grades.total and average, then compare the average using else if conditions.
#include <stdio.h> #include <conio.h> main() { int comp,phy,chem,total,avg; clrscr(); printf("Computer : "); scanf("%d",&comp); printf("Physics : "); scanf("%d",&phy); printf("Chemistry : "); scanf("%d",&chem); total = comp + phy + chem; avg = total / 3; printf("\nTotal = %d",total); printf("\nAverage = %d",avg); if(avg >= 90) { printf("\nGrade : A+"); } else if(avg >= 75) { printf("\nGrade : A"); } else if(avg >= 60) { printf("\nGrade : B"); } else if(avg >= 40) { printf("\nGrade : C"); } else { printf("\nFail"); } getch(); }
else if statement to determine the student's grade based on the average marks.