Professional Documents
Culture Documents
Lecture 19
Lecture 19
02
C Language
LECTURE 19
04
Today’s Agenda
01 NESTED if
02
Some Examples
What is nested “if”
if(<test_cond>)
{ if ( condition )
if(<test_cond>)
if(<test_cond>) {
{
{ if ( condition )
....
.... {
....
.... ....
}
} ....
else
else }
{
{ else
if(<test_cond>)
.... {
{
.... ....
....
} ....
....
} }
}
else }
else
{ else
{
if(<test_cond>) {
....
{ ....
....
.... ....
}
.... }
}
}
else
{
....
....
}
}
Is “nested if” is compulsory?
WAP to accept 3 integers from the user and find out the GREATEST number
amongst them. Assume that the user will input UNIQUE numbers only. Don’t
use any LOGICAL OPEARATOR
Solution
#include <stdio.h>
#include <conio.h>
void main() 10 10 10
{
int a, b, c;
clrscr();
a b c
printf("Enter 3 int:");
scanf("%d %d %d", &a, &b, &c);
if(a > b) 10 10 10
{
if(a > c) a b c
printf("%d is greatest", a);
else
}
printf("%d is greatest", c);
10 10 10
else
{ a b c
if(b > c)
printf("%d is greatest", b);
else
printf("%d is greatest", c); 10 10 10
}
} a b c
Exercise
Write a program to accept marks in three subjects from the user and check
whether the user is eligible for admission or not. Do not use any logical
operator
Solution
void main() else
{ printf("Admission not Given");
int p, c, m; }
clrscr(); else
printf("Enter marks of p, c, m:"); printf("Admission not Given");
scanf("%d %d %d", &p, &c, &m); }
if(p >= 50) else
{ printf("Admission not Given");
if(c >= 55) }
{ else
if(m >= 60) printf("Admission not Given");
{ getch();
if(p + c + m >= 220) }
printf("Admission Given");
else if(p + m >= 130)
printf("Admission Given");
Exercise
void main()
{
int p, c, m;
clrscr();
printf("Enter 3 int:");
scanf("%d %d %d", &p, &c, &m);
if((p >= 50 && c >= 55 && m >= 60)
&& (p + c + m >= 220 || p + m >=
130))
printf("Admission Given");
else
printf("Admission not Given");
gethc();
}
Think!
In general there is no compulsion but while using nested if we should provide corre-
sponding elves to the every if statement otherwise the output of the program will be ab-
surd or no output for some test conditions
Exercise
Thank you