C Decision Making - Branching

 

The ELSE If Ladder

When a series of many conditions have to be checked we may use the ladder else if statement which takes the following general form.
If (condition1) 
Statement – 1; 
Else if (condition2) 
Statement2; 
Else if (condition3) 
Statement3; 
Else if (condition) 
Statement n; 
Else 
Default statement; Statement-x; 
        

Syntax description

This construct is known as if else construct or ladder.
The conditions are evaluated from the top of the ladder to downwards. As soon on the true condition is found, the statement associated with it is executed and the control is transferred to the statement – x (skipping the rest of the ladder.
When all the condition becomes false, the final else containing the default statement will be executed.

ladder If-else program

#include < stdio.h > 
void main ( ) 
{ 
int marks:
printf (“Enter marks\n”); 
scanf (“%d”, & marks);
If (marks < =100 && marks > = 70 ) 
printf (“\n Distinction”); 
else if (marks > = 60) 
printf(“\n First class”); 
else if (marks > = 50) 
printf (“\n second class”); 
else if (marks>=35) 
printf (“\n pass class”); 
else 
printf (“Fail”); 
}
        

Program description

The above program checks a series of conditions. The program begins from the first if statement and then checks the series of conditions it stops the execution of remaining if statements whenever a condition becomes true.
In the first If condition statement it checks whether input value is 100 & >70.If both conditions are true it prints distinction. Instead if the condition fails then program control is transferred to the next if statement through else statement & now it checks whether next condition given is true it prints first class and comes out of If else chain to end of the program on other hand if this condition also fails, control is transferred to next if statements program execution continues till end of the loop & executes default else statement fails & stops & program.

 

Make Comments..!!


Oops!! No posts from user.

Download Android App