Nested if else statement in c
Nested if else : An Entire if else construct within either the body of the if statement or the body of else statement. That is called as " nesting " of ifs.
Program For Nested if else
#include<stdio.h>
int main(){
int x;
printf("Enter Only 1 or 2 Numbers:\n");
scanf("%d",&x);
if ( x==1)
printf("Your Enter Number is Equal to 1");
else{
if(x==2)
printf("the number is equal to 2");
else
printf("the number is not Equal To 1 or 2");
}
}
The above Programs Can be nested if else statement process. If we enter the number is equal to 1 .it's true go to next statement printf().otherwise goes to the else statement, but here can having also if statement. The if statement is true then process to printf(). otherwise else statement is executed.
Use of Logical Operators in Nested if else
The Logical operators are Process in Nested Else - if statement . The Logical Operator are &&, || , ! . There are read as 'AND' 'OR' 'NOT' Respectively.
Program For Logical Operator Using Nested If else Statement in c Program ( Percentage Problem Solve ).
#include<stdio.h>
int main()
{
int n1,n2,n3,n4,n5,n6,per;
printf("Enter The Marks in 6 Subject:\n");
scanf("%d%d%d%d%d%d",&n1,&n2,&n3,&n4,&n5,&n6);
per=(n1+n2+n3+n4+n5+n6)/6;
if(per>=60)
printf("Good Marks");
else{
if(per>=50)
printf("Average Marks");
else{
if(per>=40)
printf("Bellow Average");
else
printf("Dull");
}
}
}
Nested else if Statement ( Another Way ) would be Like Bellow Program.
Sample Example Program
Suppose ,
if ( i==2 ) if( i==2)
printf(" Hello "); printf("Hello ");
else{ elseif( j==2 )
if(j==2) printf("World");
printf(" world ");
}
Thanks For Reading More Information Nested If Else. Any Doubts Comment. Like This Post.
No comments:
Post a Comment