Programming in "C‟ | Describe use of else-if ladder with suitable example.

else if ladder is used to take a multipath decision. It is used in a program when there are more than one conditions are involved. Conditions are evaluated from the top to the bottom. As soon as the true condition is found, the statement associated with it is executed and the control is transferred to the statement-x. When all the conditions become false, then the final else containing the default statement will be executed. 

Example: 
int per=65;
if(per>=75)
printf("Distinction Class");
 else if(per<75 && per>=60)
        printf("First Class");
        else if(per<60 && per>=45)
    printf("Second Class");
    else if(per<45 && per>=35)
           printf("Pass Class");
           else
           printf("Fail");

In the above example, per variable is initialize to 65. In else if ladder first condition is checked with value of per. The condition is false so control moves to second condition. Second condition is true so its
associated statement is executed and control passes out of ladder. 

Post a Comment

0 Comments