You are on page 1of 15

01

02

C Language
LECTURE 19
04
Today’s Agenda

01 NESTED if

02
Some Examples
What is nested “if”

If we write an entire if-else construct within


either the body of the if statement or the body of an else statement.

This is called nesting of if.


There is no limit on how deeply the if and the else can be nested.
The if statement can take any of the following forms:

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?

o There is no restriction that the programmer should always choose


the nested if statement it depends upon the logic and situation of
the desired problem

o If we have a problem shown below that write the below statement


without using the logical operator or the vice versa write the be-
low program using logical operator so the below two codes are the
same one is with logical operators and another one is without logi-
cal operator both codes will give the same output on execution
Nested if with logical operators

Thus the following 2 if statements are exactly same :

Can you write the same if without &&?

if(<test_cond1> && <test_cond2>) if(<test_cond1>)


{ {
.... if(<test_cond2>)
.... {
} ....
....
}
}
Exercise

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

Admission to a professional course is given according to the following criteria:


Marks in physics should be greater than or equal to 50 and marks in chemistry should
be greater than or equal to 55 and marks in maths should be greater than or equal to
60 and total in all three subjects should be greater than or equal to 220 or total in
maths and physics should be greater than or equal to 130

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

Previous program using logical operator


Solution

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!

Is it compulsory in nested if that every if there should be a corresponding else also?

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

An insurance company provides insurance to it’s employees according to the following


criteria:
1. If the employee is married.
2. If the employee is unmarried, male and above 35 years of age
3. If the employee is unmarried, female and above 30 years of age
In all the other cases insurance is not given.
WAP to ask the user to input age, gender and marital status and check whether the user
is eligible for insurance or not. DO NOT USE ANY LOGICAL OPERATOR
End of Lecture 19
For any queries mail us @: scalive4u@gmail.com
Call us @ : 0755-4271659, 7879165533

Thank you

You might also like