Nested If-else
- In some situation ,nested if -else statement will occur and that would be good for that situation.
- This statement are nothing but same if-else statement with an another if-else statement in their body.
- This helps you to perform multiple actions in a single instruction.
- What makes nested statement to come in action?
Because, there are some problem which has to be solve by them, and for that situation nested statements becomes efficient. - Lets first see its syntax and then we will solve One problem regarding to nested statements.
Syntax
if(condition)
{
//body if block
}
else
{
if(condition)
{
//block of if
}
else{ //else block with another else statement.
if(condition){
// if block.
}
else{
// default statement.
//if no condition matches than this block will get executed
}
}
}
if(condition) { //body if block } else { if(condition) { //block of if } else{ //else block with another else statement. if(condition){ // if block. } else{ // default statement. //if no condition matches than this block will get executed } } }
Lets solve a problem , of grades of student.
Q:take an percentage of student as an input and tell them what is their grade.
below displayed table has percentage and grade corresponding to their marks
Percentage | Grade |
---|---|
above 90 | |
80 to 90 | |
70 to 80 | |
60 to 70 | |
50 to 60 | |
40 to 50 | |
below 40 |
Solution:
#include<stdio.h>
void main()
{
int per;
printf("Enter your percentage:");
scanf("%d",&per);
if(per > 90){
printf("Your grade is O");
}
else{
if(per > 80 && per <= 90){
printf("Your grade is A");
}
else{
if(per > 70 && per <= 80)
{
printf("Your grade is B");
}
else{
if(per > 60 && per <= 70)
{
printf("Your grade is C");
}
else{
if(per > 50 && per <= 60)
{
printf("Your grade is D");
}
else{
if(per > 40 && per <= 50)
{
printf("Your grade is E");
}
else
printf("Fail");
}
}
}
}
}
}
#include<stdio.h> void main() { int per; printf("Enter your percentage:"); scanf("%d",&per); if(per > 90){ printf("Your grade is O"); } else{ if(per > 80 && per <= 90){ printf("Your grade is A"); } else{ if(per > 70 && per <= 80) { printf("Your grade is B"); } else{ if(per > 60 && per <= 70) { printf("Your grade is C"); } else{ if(per > 50 && per <= 60) { printf("Your grade is D"); } else{ if(per > 40 && per <= 50) { printf("Your grade is E"); } else printf("Fail"); } } } } } }
Output: For all types of percentage
Enter your percentage:95
Your grade is O.
Enter your percentage:85
Your grade is A.
Enter your percentage:75
Your grade is B.
Enter your percentage:65
Your grade is C.
Enter your percentage:55
Your grade is D.
Enter your percentage:45
Your grade is E.
Enter your percentage:31
Fail.
Your grade is O.
Enter your percentage:85
Your grade is A.
Enter your percentage:75
Your grade is B.
Enter your percentage:65
Your grade is C.
Enter your percentage:55
Your grade is D.
Enter your percentage:45
Your grade is E.
Enter your percentage:31
Fail.
- Above code is just looking lengthy but, when you will read the code, you will find it easy.
- Explanation:
- For: percentage = 65
- when condition was checked at first if block it was evaluated false.
- As you know control goes to else block, so it went to else block and in that else block there is an if block.
- Condition was checked in that if block and it also evaluated to false, and again control went to next else block
- In that else block there was an another if block, as condition was checked for
that if block, but again it evaluated to false, and control went to next else block. - That else block also contain an if block, as condition was checked for that if block but ,This time condition was evaluated to true and it printed that 'Your grade is C'
- Try to trace grade for other percentage by your own, doing this you will understand the nested statement better.
- Try to do more programs on nested statement to understand it better.
0 Comments
Post a Comment