You are on page 1of 23

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : T.Sudhakar
Designation : Lecturer in Computer Engg.
Branch : Computer Engg.
Institute : S.S.Govt. Polytechnic, Zaheerabad
Year/Semester : III Semester
Subject Name : UNIX & C
Sub. Code : CM-304
Major Topic : Programming Constructs
Sub-Topic : Conditional Statements
Duration : 100 Min
Teaching Aids : PPT, Animation.
CM304.43TO44 1
Recap
Syntax of if statement is Syntax of if.. else statement

if(condition) if (condition)
{ {
statements ; statements block1;
… }
} else
next statement; {
statements block2;
}
next statements;

CM304.43TO44 2
Objectives

On completion of this class, you would be able to


know

• Programs on if .. else Statement

• Nested if Statement

CM304.43TO44 3
Write C program to determine whether the
entered year is leap year or not.
• The year is leap, if it is divisible by 4
and it should not be divisible by 100
or if it is divisible by 400.
So, the condition is
year%4 == 0 && year%100 != 0 || year%400 == 0

CM304.43TO44 4
PROGRAM
#include<stdio.h>
main()
{
int year;
printf(“Enter the year:\n”);
scanf(“%d”,&year);
if(year%4 == 0 && year%100 != 0 || year%400 == 0)
printf(“It is leap year”);
else
printf(“It is not leap year”);
}

CM304.43TO44 5
Output::1
DISPLAY
Enter the year:
1900
It is not leap year

year

1900
Memory Contents

CM304.43TO44 6
Output::2
DISPLAY
Enter the year:
1900
It is not leap year

year

2008

Memory Contents

CM304.43TO44 7
Nested if .. else Statement
• Suppose if block or else block contains another
if statement or if..else statement, the
construct is known as Nested if .. Else
Statement.

• Useful when series of decisions are invoked.

CM304.43TO44 8
Nested if .. else Statement
Contd..
if(expression1) else
{ {
if(expression2) if(expression3)
{ {
block -3
block -1
}
}
else
else {
{ block -4
block -2 }
} }
} statement x;
CM304.43TO44 9
F
Expression 1 T

Expression 2
T
Block-3
F

Block-4
Expression 3

F
T
Block-1 Block-2

Flow chart of
Nested if .. else Statement-x;
Statement CM304.43TO44 10
To find the largest among three numbers.

Algorithm
• Read three numbers a, b, c
• If a > b, if a > c ,display a is largest otherwise c is
largest
• If b > largest, place the value of b into largest
• If c > largest, place the value of c into largest
• Then display the value of largest

CM304.43TO44 11
Start

Read a,b,c

false true
true true
b>c a>b a>c

false false

b is largest c is largest c is largest a is largest

Flow chart to find the largest among three


numbers.
CM304.43TO44 12
PROGRAM

#include<stdio.h>
main()
{
int a, b, c;
printf(“Enter a, b, c :\n”);
scanf(“%d%d%d”,&a,&b,&c);

CM304.43TO44 13
PROGRAM contd..
if(a>b)
{
if(a>c)
printf(“a is largest”);
else
printf(“c is largest”);
}
else
{
if(b>c)
printf(“b is largest”);
else
printf(“c is largest”);
}
}
CM304.43TO44 14
PROGRAM
DISPLAY
#include<stdio.h>
main() Enter a, b, c :
{
int a, b, c; 89 69 98
printf(“Enter a, b, c :\n”);
scanf(“%d%d%d”,&a,&b,&c); c is largest
if(a>b)
{
if(a>c)
printf(“a is largest”);
else
printf(“c is largest”);
}
else a b c
{
if(b>c) 89 69 98
printf(“b is largest”);
else
printf(“c is largest”); Memory Contents
}
CM304.43TO44 15
}
PROGRAM DISPLAY
#include<stdio.h>
main() Enter a, b, c :
{
int a, b, c; 89 69 36
printf(“Enter a, b, c :\n”);
scanf(“%d%d%d”,&a,&b,&c); a is largest
if(a>b)
{
if(a>c)
printf(“a is largest”);
else
printf(“c is largest”);
}
else a b c
{
if(b>c) 89 69 36
printf(“b is largest”);
else
printf(“c is largest”); Memory Contents
}
CM304.43TO44 16
}
PROGRAM DISPLAY
#include<stdio.h>
main() Enter a, b, c :
{
int a, b, c; 16 98 36
printf(“Enter a, b, c :\n”);
scanf(“%d%d%d”,&a,&b,&c); b is largest
if(a>b)
{
if(a>c)
printf(“a is largest”);
else
printf(“c is largest”);
}
else a b c
{
if(b>c) 16 98 36
printf(“b is largest”);
else
printf(“c is largest”); Memory Contents
}
CM304.43TO44 17
}
Write a program to check whether the given
number is single digit +ve number or not.
#include< stdio.h>
void main()
{
int n;
printf(“Enter any number \n”);
scanf (“%d”, &n);

CM304.43TO44 18
if (n<10)
{
if (n>0)
printf (“%d is a single digit +ve”, n);
else
printf( “%d is single digit –ve”, n);
}

else
printf (“%d is multidigit number”n);
}

CM304.43TO44 19
Summary

In this class, we have learnt about..

• Programs on if .. else Statement


• Nested if Statement

CM304.43TO44 20
Quiz

1. Rewrite each of the following without using the


compound relations
a) if (grade<=59 && grade>=50)
second = second + 1;
Ans:-
if(grade<=59)
{
if(grade>=50)
second = second + 1;
}
CM304.43TO44 21
b) if ((M1 > 60 && M2 > 60)
printf(“Admitted”);
else
printf(“Not admitted”);

Ans:- if (M1 > 60)


{
if(M2 >60)
printf(“Admitted”);
else
printf(“Not admitted”);
}
else
printf(“Not Admitted”);

CM304.43TO44 22
Frequently asked Questions

1.Write C program to determine whether given year


is leap or not?
2. Explain the use of Nested if..else Statements with
examples?
3. Write a C program to find the greatest among
three numbers using Nested if..else Statement?

CM304.43TO44 23

You might also like