else-if ladder


The else-if ladder Statement.

An else if ladder is a special case of nested if statement where nesting take place only in the else part when an action has to be selected based on range of value then this statement is used.
Syntax:-
   if(condition)
  stat f1
  else-if(conditon 2)
  stat s1
  else-if(condition 3)
  stat s2
  else-if(condition 4)
  stat s4
  ..........
  ............
  else(condition n-1)
  stat n-1
  else
  stat n

Flowchart:-

else if ladder

Example:-

MarksGrades
More than or equal 90%O(Outstanding)
80 to 89A
70 to 79B
60 to 69C
50 to 59D
40 to 49E
Less than 40Fail

W.A.P for above condition.

#include<stdio.h>
#include<conio.h>
    void main()
     {
    int marks;
    printf(“enter the marks\n”);
    scanf(“%d”,&marks);
    if(marks>=90)
    printf(“ O”);
    else if(marks>=80&&marks<89)
    printf(“A”);
    else if(marks>=70&&marks<8`0)
    printf(“B”);
    else if(marks>=60&&marks<70)
    printf(“C”);
    else if(marks>=50&&marks<60)
    printf(“D”);
    else if(marks>=40&&marks<50)
    printf(“E”);
    else
    printf(“F”);
    getch();
 }

Output

enter the marks
75
B

W.A.P to print three no. and find greater.

 #include<stdio.h>
 #include<conio.h>
  void main()
  {
   int a,b,c;
   printf("enter three no");
   scanf("%d %d %d",&a,&b,&c);
  if(a>b && a>c)
  {
   printf("a is greater no.");
  }
   elseif(b>a && b>c)
  {
   printf("b is greater no.");
  }
   else
  {
   printf("c is greater no");
  }
   getch();
  }

Output

enter three no
2 4 6
c is greater no

W.A.P to find gross salary.

 #include<stdio.h>
 #include<conio.h>
  void main()
 {
   int bs,ta,da,hra,gs;
   printf("enter bs");
   scanf("%d",&bs);
   if(bs>20000)
 {
   ta=bs*0.20;
   da=bs*0.20;
   hra=bs*0.20;
 }
   elseif(bs>15000 && bs<=20000)
 {
   ta=bs*0.15;
   da=bs*0.15;
   hra=bs*0.15;
 }
   elseif(bs>10000 && bs<=15000)
 {
   ta=bs*0.10;
   da=bs*0.10;
   hra=bs*0.10;
 }
  else
 {
   ta=bs*0.5;
   da=bs*0.5;
   hra=bs*0.5;
 }
   gs=ta+da+hra+bs;
   printf("%d",gs);
   getch();
 }

Output

enter bs
20000
32000