Need of nested if-else control structure.
All the problems may not be solved using if, if-else and
logical expressions.
Some times we need to place an if-else within either if or
else part of another if-else control structure called nested if-else control
structure.
It gives additional flexibility in conditional execution.
Explanation through example:
Specification:
Accept three numbers and print the biggest number.
Program:
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter three numbers:\n");
scanf("%d%d%d",&a,&b,&c);
if(a==b && b==c)
printf("Equals");
if(a>b && a>c)
printf("Biggest number is %d",a);
if(b>a&&b>c)
printf("Biggest number is %d",b);
if(c>a&&c>b)
printf("Biggest number is %d",c);
}
Execution test cases:
Input |
Expected |
Actual |
Test result |
a b c |
|||
10 10 10 |
Equals |
Equals |
pass |
90 50 60 |
90 |
90 |
pass |
20 80 75 |
80 |
80 |
pass |
40 30 92 |
92 |
92 |
pass |
60 60 20 |
60 |
No output |
fail |
-10 -20 -10 |
-10 |
No output |
fail |
-10 0 0 |
0 |
No output |
fail |
60 10 10 |
60 |
60 |
pass |
20 40 20 |
40 |
40 |
pass |
The above example shows that we cannot print the biggest
number out of the given three numbers in all the cases using the above logic.
(using if and logical expressions).
Rectified program through nested if-else:
Program:
#include<stdio.h>
void main()
{
int math,physics,chemistry;
printf("Enter the marks in math, physics and chemistry:\n");
scanf("%d%d%d",&math,&physics,&chemistry);
if(math>=50 && physics>=50 && chemistry>=50)
printf("Pass");
else
In case if if-else
if(math>=50 && physics>=50 && chemistry>=50)
printf("Pass");
else
printf("Fail");
}
Execution test cases:
Input |
Expected |
Actual |
Test result |
a b c |
|||
10 10 10 |
Equals |
Equals |
pass |
90 50 60 |
90 |
90 |
pass |
20 80 75 |
80 |
80 |
pass |
40 30 92 |
92 |
92 |
pass |
60 60 20 |
60 |
60 |
pass |
-10 -20 -10 |
-10 |
-10 |
pass |
-10 0 0 |
0 |
0 |
pass |
60 10 10 |
60 |
60 |
pass |
20 40 20 |
40 |
40 |
pass |
It is proved that nested if-else is flexible than simple if
and if-else.
Specification1:
Accept any three numbers and print the smallest number.
Program:
#include<stdio.h>
void main()
{
int n,first,second,third;
printf("Enter any three digit number:");
scanf("%d",&n);
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
if(first==second && second==third)
printf("All the digits are equal");
else
{
if(first>second && first>third)
printf("Biggest digit %d",first);
else
{
if(second>third)
printf("Biggest digit %d",second);
else
printf("Biggest digit %d",third);
}
}
Execution1:
Enter three numbers: 20 40 -10
Smallest number is -10
Execution2:
Enter three numbers: 20 10 10
Smallest number is 10
Specification2:
Accept the marks of a student in 3 subjects and print
whether pass or fail, if fail print the details. Condition to pass is that, the
student must get 50 or more marks in every subject.
Program:
#include<stdio.h>
main()
int m,p,c;
printf("enter the marks in m,p and c:");
scanf(" %d %d %d",&m,&p,&c");
if(m>35)
if(p>35)
if(c>35)
printf("pass");
else
printf("fail");
}
Execution1:
Enter the marks in math, physics and chemistry:
60 75 85
Pass
Execution2:
Enter the marks in math, physics and chemistry:
10 75 15
Fail in mathematics
Fail in chemistry
Specification3:
Accept the gender, age, percentage of marks of a person and
print whether selected or not. Here selections criteria are that, a first class
male with the age between 25 and 34, if not selected then print the details.
#include<stdio.h>
void main()
{
int age;
float percent;
char gender;
printf("Enter the gender m/f :");
scanf("%c",&gender);
printf("Age:");
scanf("%d",&age);
printf("Enter the percentage in qualifying exam:");
scanf("%f",&percent);
if(gender==’m’ && percent>=60 && age>=25 && age<=34)
printf("Selected");
else
{
if(gender==’f’)
printf("Required male not a female.");
if(age<25 age="" if="" n="" printf="" under="">34)
printf("\n Over age");
if(percent<60 code="" enough="" n="" not="" percentage="" printf="">60>25>
Execution1:
Enter the gender m/f: m
Age: 30
Enter the percentage in qualifying exam: 75
Selected
Execution2:
Enter the gender m/f: f
Age: 30
Enter the percentage in qualifying exam: 50
Required male not a female
Not enough percentage
specification4:
Accept any 3 digit number and print the biggest digit.
Program
#include<stdio.h>
void main()
{
int n,first,second,third;
printf("Enter any three digit number:");
scanf("%d",&n);
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
if(first==second && second==third)
printf("All the digits are equal");
else
{
if(first>second && first>third)
printf("Biggest digit %d",first);
else
{
if(second>third)
printf("Biggest digit %d",second);
else
printf("Biggest digit %d",third);
}
}
Execution1:
Enter any three digit number: 333
All the digits are equal
Execution2:
Enter any three digit number: 452
Biggest digit 5
Specification5:
Accept the marks of a student in 6 subjects and print the
total, average and result.
Program:
#include<stdio.h>
void main()
{
int m1,m2,m3,m4,m5,m6,tot;
float avg;
printf("Accept the marks of a student in 6 subjects:\n");
scanf("%d%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5,&m6);
tot=m1+m2+m3+m4+m5+m6;
avg=(float)tot/6;
printf(" Total marks %d",tot);
printf("\n Average marks %f",avg);
printf("\n Result:");
if(m1<50 ail="" avg="" else="" if="" m2="" m3="" m4="" m5="" m6="" printf="">=75)
printf("Grade A+");
else
{
if(avg>=60)
printf("Grade A");
else
{
if(avg>=50)
printf("Grade B");
else
printf("Grade C");
}
}
}
}
50>
Execution1:
Enter the mark of a student in 6 subjects:
67 70 65
55 78 80
Total marks 415
Average marks 69.166667
Result: Grade A
0 comments:
Post a Comment