In C programming language the else if ladder is a way of putting multiple ifs together when multipath decisions are involved. It is a one of the types of decision making and branching statements. A multipath decision is a chain of if’s in which the statement associated with each else is an if. The general form of else if ladder is as follows –
if ( condition 1) { statement - 1; } else if (condtion 2) { statement - 2; } else if ( condition n) { statement - n; } else { default statment; } statement-x;
This construct is known as the else if ladder.The conditions are evaluated from the top of the ladder to downwards. As soon as a 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 n conditions become false, then the final esle containing the default statement will be executed.
Below is the sample C program of the if – else ladder statement in which the color is to be selected by using the if – else ladder –
#include<stdio.h> #include<string.h> void main() { int n; printf(" Enter 1 to 4 to select random color"); scanf("%d",&n); if(n==1) { printf("You selected Red color"); } else if(n==2) { printf("You selected Green color"); } else if(n==3) { printf("You selected yellow color"); } else if(n==4) { printf("You selected Blue color"); } else { printf("No color selected"); } getch(); }
Note – C programming language is a structured language; so it will be better to form blocks in ‘if else ladder’ as this makes the programmer to understand the language better and reduces the confusion and chaos. Also it will make the others easy to understand your program. It is also necessary to align the opening and the closing braces of the block.
Very nice tutorial
i got one error.what can i do…
nice program
i got one 2 errors….. number 4 and 30
write a program to enter total sales you will calculate discount as per given condition.
sales discount
2000 && 40000 && 60000 && 80000 10%
you will print total sales ,discount amount, pay bill amount.
Most help articles on the web are inaccurate or inhrteoenc. Not this!
We could’ve done with that insight early on.
Nice tutorial.
humko chodo pls.
i want more information about c programing
i want more examples on else if ladder with output
Here is the C Program to print grade of a student using If Else Ladder Statement
if else program
write a program to change c’s background
please try it
I want an another else if prgm with output
A plngsiealy rational answer. Good to hear from you.
hi please send number of programs in if and if else and if ladder and nested program in my mail id
I want a very examples with an output
I want very small examples with output
What is the use of statement x ?
Please write algorithm of if else ladder statement.
Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up.