Professional Documents
Culture Documents
Nested If Else in C
Nested If Else in C
programmerdouts.blogspot.com/2019/06/nested-if-else-in-c-language.html
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
1/6
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
}
}
Percentage Grade
above 90 O
80 to 90 A
70 to 80 B
60 to 70 C
50 to 60 D
40 to 50 E
2/6
below 40 Fail
Solution:
3/6
#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");
}
}
}
}
}
}
Note: If you have code of one line in your if block you can write it without
using the curly braces, In Above code we have used the braces just because
you should not get confuse.
Practice Programs
5/6
Programs Regarding If-else Statements
Program Regarding Nested If - statements
Programs Regarding loops
Further Topics
6/6