if else Ladder: if else if ladder is a control structure in C programming used to test a series of conditions sequentially. It allows programmer to execute a different block of code depending on multiple if else statements arranged in a ladder fashion. If any of the condition evaluate to be true the appropriate code block will be executed and if-else ladder will terminate.
The if else ladder is an extended form of if else statement. It is used when programmer need to perform multiple activity for different conditions.
In if-else-if ladder statement, the block of code will be executed if the condition is true otherwise it continue for to test other else-if condition, if condition is true then statement defined in else if block will be processed. Finally if none of the mentioned condition is true then the statement defined in else block will be executed. There are multiple if else blocks possible.
Let Understand how if-else-if Ladder works in C programming with the help of given syntax below.
if else Ladder in C Language
Syntax : if else if Ladder statement
/* following syntax is the if-else-if ladder in C programming Language */
if (Condition1)
{
Statement1; /* Execute statement1 if Condition1 is true*/
}
else if(Condition2)
{
Statement2; /* Execute statement2 if Condition2 is true*/
}
.
.
.
else if(ConditionN)
{
StatementN; /* Execute statementN if ConditionN is true*/
}
else
{
default_Statement; /* Execute default_Statement if the all conditions are false */
}
Working of if-else-if ladder:
Initially at the begining control falls into the if block. After that flow jumps to Condition1.
The Condition1 is tested, if the condition1 tested is true then the Statement1 present in if body is executed.
if Condition1 tested is false then control jumps to else if(Condition2).
again it check the Condition2 , if Condition2 tested is true then statement2 is executed.
The process contines to the last else-if(ConditionN) ,if last ConditionN is false then it executes the else block.
i.e. executes default_statement and control comes out of the if-else-if ladder statement.
Given Diagram is for the flowchart of the if-else Ladder statement.
The Flow chart shows working of if-else-if Ladder in C Programming.
if the Condition1 is TRUE then the Statement1 will be executed and control goes to next statement in the program following if-else-if ladder.
If Condition1 tested is FALSE only then Condition2 will be tested, if Condition2 is TRUE then Statement2 will be executed and control goes to next statement .
Similarly, if Condition2 is FALSE then next condition will be checked and the process continues.
If all the conditions in the if-else-if ladder are evaluated to FALSE, then Default Statement will be processed.
Example 1: C program to illustrates the use of if-else-if ladder statement .
/* C program to find maximum number out of 4 given numbers */
#include<stdio.h>
int main()
{
int p,q,r,s;
printf("\n Enter any four numbers");
scanf("%d%d%d%d",&p,&q,&r,&s);
if(p>q && p>r && p>s) /*test p largest */
{
printf("\n %d is Maximum number",p);
}
else if (q>p && q>r && q>s) /*else test q is largest*/
{
printf("\n %d is Maximum number",q);
}
else if (r>p && r>q && r>s) /*else test r is largest */
{
printf("\n %d is Maximum number",r);
}
else
{
printf("\n %d is Maximum number",s);
}
return 0;
}
Output:
Enter any four numbers
5
7
1
8
8 is Maximum number.
Program Explanation:
The above program illustrates the use of if-else-if ladder statement to find maximum number out of given four number.
1. In the above program, we have declared integer variables int p,q,r,s ; to store integer values.
2. Then, we have used printf("\n Enter any four numbers");
scanf("%d%d%d%d",&p,&q,&r,&s);
Which shows the message "Enter any four number" on console and control waits until we do not enter any four number, then we entered numbers are
5
7
1
8
The Entered numbers get stored in variable p, q, r, s respectively by scanf("%d%d%d%d",&p,&q,&r,&s);
3. Then, we have used if with a test-expression to check whether the number in variable p is the maximum by using if(p>q && p>r && p>s).
Here the test expression evaluates to false i.e. p=5 is not greater than any of the variables so control jumps to next else-if block
4. and test the condition else if (q>p && q>r && q>s). which is again false because q=7 is not greater than any of the values or variable, control again jumps to next else-if block.
5.and start executing the condition else if (r>p && r>q && r>s). which is again evaluates to false i.e. r=1 is not greater than any other values and finally else block executes. Control transfer to else block.
5.else the statements inside the else block executes
i.e. printf("\n %d is Maximum number",s);
and gives final output 8 is Maximum number
Previous Topic:-->> if else statement in C || Next topic:-->>Nested if else in C.