{ A destination to learn coding }

Conditional Statements

If-else-if ladder

If there are more than 2 possibilities of outcome you can use the if-else-if ladder.

Modifying the previous code using if-else if
#include<stdio.h>
int main()
{
int a,b,sum;
printf("Enter any two integers\n");
scanf("%d%d",&a,&b);

sum=a+b;

/* in the code below we have 3 possibilities */
if(sum>50)
  printf("The sum is greater than 50\n");

else if(sum==50)
  printf("The sum is equal to 50\n");

else
  printf("The sum is less than 50\n");

return 0;
}

In the above code, if the if expression is true, the if statement will be executed. If not, the else if statement will be evaluated. If this is false as well, the else statement will be executed.

#include<stdio.h>
int main()
{
float a;
printf("Enter your percentage\n");
scanf("%f",&a);

if((a>0)&&(a<40))
  printf("Fail");

else if((a>=40)&&(a<50)) /*using logical operators*/
  printf("Pass Class");

else if((a>=50)&&(a<60))
  printf("Second Class");

else if((a>=60)&&(a<75))
  printf("First Class");

else if((a>=75)&&(a<90))
  printf("Distinction");

else if((a>=90)&&(a<=100))
  printf("Merit");

else printf("Invalid Input");

return 0;
}
Try this
Finding greatest in three numbers using logical operator and if-else statements
C Codes