4. nested-if statement

Nested if statement contains the no. of if conditions without else part. If the condition is true or false it checks the remaining conditions also.
syntax:-
if(<condition1>)
if(<condition2>)
Statement1;
if(<condition3>)
if(<condition4>)
statement2;
if(<condition5>)
if(<condition6>)
statement3;
If the condition1 and condition2 are true then statement1 will be executed then it goes to condition3. If the condition1 is false then it will check the condition3,condition4 are true then statement 2 will be executed then it goes to the condition5. If the condition3 is false then it goes to the condition 5. If condition 5,condition 6 are true statement 3 executed and so.on. If condition 5 is false it goes to the next if condition and so.on. for remaining.
/* Write a program to calculate the monthly telephone bills as per the following rule:
Minimum Rs. 200 for upto 100 calls.
Plus Rs. 0.60 per call for next 50 calls.
Plus Rs. 0.50 per call for next 50 calls.
Plus Rs. 0.40 per call for any call beyond 200 calls. */
#include<stdio.h>
main()
{
int noc;
float bill;
clrscr();
printf("Enter no.of calls:");
scanf("%d",&noc);
if(noc>=0)
if(noc<=100)
bill=200;
if(n>100)
if(n<=150)
bill=200+(noc-100)*0.60;
if(n>150)
if(n<=200)
bill=200+30+(noc-150)*0.50;
if(n>200)
bill=200+30+25+(noc-200)*0.40;
printf("\nBill = %.2f",bill);
getch();
}
syntax:-
if(<condition1>)
Statement1;
if(<condition2>)
statement2;
if(<condition3>)
statement3;
If the condition1 is true if then statement1 will be executed. If the condition1 is true or false then it will check the condition2, if it is true then statement 2 will be executed if condition 1 is false and condition 2 is true then statement 2 will be executed. condition1 and condition2 are true statement 1 and statement 2 will be executed and so. On for remaining.
/*WAP read service number from the user and print the corresponding service name?
If service Number is 100, print police.
If service Number is 101, print Fire.
If service Number is 108, EMRI
Else Invalid*/
#include<stdio.h>
main()
{
int sn;
printf("Enter a service number:");
scanf("%d",&sn);
if(sn==100)
printf("Police");
if(sn==101)
printf("Fire");
if(sn==108)
printf("EMRI");
if(sn!=100 && sn!=101 && sn!=108)
printf("Invalid Service.");
}
nested if-else statement:-
if(<condition1>)
{
if(<condition2>)
Statement1;
else
Statement2;
}
else
{
if(<condition3>)
statement3;
else
statement4;
}
statement n;
If the condition 1 is true it goes to the condition 2 if it true then statement 1 is executed. If the condition 2 is false then statement 2 is executed. If the condition 1 is false it goes to the condition 3 if it is true then statement 3 is executed. If the condition 3 is false then statement 4 is executed. finally it executes statement n.
/*WAP read transaction type, bank balance, transaction amount, calculate the current balance.*/
#include<stdio.h>
main()
{
float bb,ta;
char tt,pn[10];
clrscr();
printf("Enter bank balance");
scanf("%f",&bb);
printf("Enter type of transaction");
fflush(stdin);
scanf("%c",&tt);
printf("\nEnter transaction amount");
scanf("%f",&ta);
if(tt=='d' || tt=='D')
{
if(ta>=50000.00f)
{
printf("Enter PAN Number");
scanf("%s",&pn);
bb=bb+ta;
}
else
bb=bb+ta;
}
else
{
if(ta<bb)
bb=bb-ta;
else
printf("Your Bank Balance is Low.");
}
printf("\nThe current balance is %f",bb);
}
if-else-if inside if-else statement:-
if(<condition1>)
{
statement1;
if(<condition2>)
statement2;
else if(<condition3>)
statement3;
else if(<condition4>)
statement4;
}
else
statement5;
If the condition1 is true then statement1 will be executed and it goes to the condition2 if it is true statement2 will be executed and skip the remaining conditions. If the condition2 is false then it will check the condition3, if it is true then statement2 will be executed and skip the remaining statements and so.on if all conditions are false there is no statement. Else part is optional. If there is no else part if all the conditions are fail there is no result. If the condition1 is false it goes to the else part and execute statement5.
WAP read 3 subject marks from the user calculate total, result and grade.
#include<stdio.h>
main()
{
int sub1,sub2,sub3,tot;
printf("Enter sub1,sub2,sub3 marks");
scanf("%d%d%d",&sub1,&sub2,&sub3);
if(sub1>34 && sub2>34 && sub3>34)
{
printf("\nStudent Pass");
tot=sub1+sub2+sub3;
if(tot>=210 && tot<=300)
printf("\nA+ Grade.\nMarks is:%d",tot);
else if(tot>=180 && tot<210) 
printf("\nA Grade.\nMarks is:%d",tot);
else if(tot>=150 && tot<180)
printf("\nB Grade.\nMarks is:%d",tot);
else if(tot>=120 && tot<150)
printf("\nC Grade. \nMarks is:%d",tot);
else
printf("\nD Grade. \nMarks is:%d",tot);
}
else
printf("\nStudent Fail");
}

No comments:

Post a Comment