For video tutorial you can see below video:
The if-else is statement is an extended version of If. The general form of if-else is as follows:
When using if...else if..else statements, there are few points to
keep in mind :-
✔An if can have zero or one else's and it must come after any else if's.
✔ An if can have zero to many else if's and they must come before the else.
✔ Once an else if succeeds, none of the remaining else if's or else's will be tested.
✔An if can have zero or one else's and it must come after any else if's.
✔ An if can have zero to many else if's and they must come before the else.
✔ Once an else if succeeds, none of the remaining else if's or else's will be tested.
The general syntax of how
else-if ladders are constructed in 'C' programming is as follows:
if(test-expression 1) {
Statement1;
} else if(test-expression 2) {
Statement2;
} else if(test-expression 3) {
Statement3;
} else if(test-expression n) {
Statementn;
} else {
default;
}
Statement x;
} else if(test-expression 2) {
Statement2;
} else if(test-expression 3) {
Statement3;
} else if(test-expression n) {
Statementn;
} else {
default;
}
Statement x;
✔ This type of structure is known
as the else-if ladder. This chain generally looks like a ladder hence it is
also called as an else-if ladder. The test-expressions are evaluated from top
to bottom.
✔ Whenever a true test-expression if found, statement associated with it is executed. When all the n test-expressions becomes false, then the default else statement is executed.
✔ Whenever a true test-expression if found, statement associated with it is executed. When all the n test-expressions becomes false, then the default else statement is executed.
Let us see the actual working with the help
of a program.
#include<stdio.h>
#include<conio.h>
void main()
{
int marks=83;
clrscr();
if(marks>75){
printf("First
class");
}
else if(marks>65){
printf("Second
class");
}
else if(marks>55){
printf("Third
class");
}
else{
printf("Fourth
class");
}
getch();
}
First class
✔ The above program prints the grade as per the marks scored in a test. We have used the else-if ladder construct in the above program.
👉 We have initialized a
variable with marks. In the else-if ladder structure, we have provided various
conditions.
👉 The value from the
variable marks will be compared with the first condition since it is true the
statement associated with it will be printed on the output screen.
👉 If the first test
condition turns out false, then it is compared with the second condition.
👉 This process will go on
until the all expression is evaluated otherwise control will go out of the
else-if ladder, and default statement will be printed.
✔ Try modifying the value and notice the change
in the output.
Summary:
👉We can also nest if-else within one another when multiple paths have to be tested.
👉It is also called as control statements because it controls the flow
of execution of a program.
👉'C' provides if, if-else constructs for decision-making statements.👉We can also nest if-else within one another when multiple paths have to be tested.
✔ The else-if ladder is used when we have to
check various ways based upon the result of the expression.
0 Comments
If any one want learn some specific program in Computer Languages, so They all can comment there program questions.