NESTED-IF Format in C lang
Posted by
Ravi Kumar at Wednesday, August 31, 2011
Share this post:
|
/*Quick Demo of NESTED if-else*/
main()
{
int i;
printf("Enter either 1 or 2");
scanf("%d",&I);
if(i==1)
printf("You would go to Heaven");
else
{
if(i==2)
printf("Hell was created with
you in mind");
else
printf("How about mother earth");
}
In the above program an IF-ELSE occurs
within the ELSE block of the first IF state
ment.Similarly,in some other programs an
IF-ELSE may occur in the IF block as well.
There is no limit on how deeply the IF'S and
ELSE'S can be nested.
Example:
/* calculation of Gross Salary*/
main()
{
float bs,gs,da,hra;
Printf("Enter basic salary");
scanf("%f",&bs);
if(bs<1500)
{
hra=bs*10/100;
da=bs*90/100;
}
else
{
hra=500;
da=bs*98/100;
}
gs=bs+hra+da;
printf("gross salary=Rs %f",gs);
}
Example:
main()
{
int a=500,b,c;
if(a>=400)
b=300;
c=200;
printf("\n%d%d",b,c);
}
OUTPUT: 300 200
Example:
main()
{
int x=10,y=20;
if(x==y);
printf(\n %d%d",x,y);
}
OUTPUT: 10 20
Explanation:
The semi-colon (;) at the end of
IF statement terminates , IF functionally,
so x and y values are get printed.