If Else Ladder in C


1.What is if else ladder in C ?

Ans:  The if else ladder are used when the user has to decide among multiple options. The C if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. if-else-if ladder is similar to the switch statement.

Simple Explanation :

In this image we can see that the police has caught the person in the car. The police will first check the license. If driver dose not license he will check the vehicle paper. If the driver dosent't have the paper he will keep the vehicle with him.

The if else ladder also works same it first check the if condition if it is false then it will go to else if if this is also wrong it will go to else and print the statement in the else block.  



Working of if else ladder :

Syntax  :

if (condition)
    statement;
else if (condition)
    statement;
else
    statement;


 Example:

1. Write a program to determine the number enter is positive, negative or number is equal to zero.

#include<stdio.h>
int main ()
{
int a;
printf("enter your favourite number");
scanf("%d",&a);
if (a>0)
{
printf("%d is positive",a);
}
else if (a<0)

{
printf("%d is negative",a);
}
else
{
printf("%d is equal to zero",a);
}
}

Output :





2. Write a to check the result of student.

#include<stdio.h>
int main ()
{
int eng,his,maths,geo,sci,add,totalmarks=5;
float divi;
printf("enter your score in english\n");
scanf("%d",&eng);
printf("enter your score in history\n");
scanf("%d",&his);
printf("enter your score in maths\n");
scanf("%d",&maths);
printf("enter your score in geography\n");
scanf("%d",&geo);
printf("enter your score in science\n");
scanf("%d",&sci);
add=eng+sci+geo+maths+his;
printf("total marks=%d\n",add);
divi=add/totalmarks;
printf("Percentage=%.2f\n",divi);
if (divi>=80 && divi<=100)
{
printf("you got grade A");
    }
    else if (divi>=60 && divi<=80)
    {
    printf("you got grade B");
}
else if (divi>=40,divi<=60)
{
printf("pass");
}
else 
{
printf("Fail");
}
}

Output :