You are on page 1of 39

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.41TO42 1
Recap

• List relational operators.

• List logical operators.

• How to define a relational expression.

• How to define logical expression.

• Give the precedence of relational and logical


operators.
CM304.41TO42 2
Objectives
On the completion of this class, you would be able
to know.
• Control Statements
• Types of Control Statements
• Branching Statements
• Conditional Statements
• if Statement
• if..else Statement

CM304.41TO42 3
Control Statements
• Generally the statements in C program are executed one
by one sequentially i.e., the flow of control is sequential.

• We get the situations where the order of execution of


statements has to be changed based on certain conditions.

• The statements which affect or change the flow of


execution are called Control Statements.

They can be classified into


• Branching Statements
• Iterative Statements or Loop Statements.

CM304.41TO42 4
Branching Statements
• These are used to transfer the flow of control to
specified part of the program.
• These are classified as:

1. Conditional branching Statements


2. Unconditional Branching
Statements

CM304.41TO42 5
1. Conditional Statements
• The control is transferred on checking the condition.

• The conditional statements use relational and logical


operators.

• In C, the following are Conditional Statements.

• if Statement
• if … else Statement
• Nested if .. else Statement
• Else if ladder
• Switch Statement

CM304.41TO42 6
Simple if Statement
• Used to execute or skip one or group of the statements for
a particular condition.
• Executes group of statements if condition is true.
• Skip them if condition is false.
• The Syntax of if statement is

if(condition)
{
statements ;

}
next statement;
CM304.41TO42 7
Flowchart of if Statement

Condition or
Statements
expression
True

False

Next Statements
CM304.41TO42 8
To determine whether candidate’s age is greater
than 17 or not. If yes, display “eligible for voting”

Algorithm
• Read the age of the candidate
• If age is greater than 17
• Then display “eligible for voting”

CM304.41TO42 9
PROGRAM
#include<stdio.h>
main()
{
int age;
printf(“Enter age of candidate:\n”);
scanf(“%d”,&age);
if(age > 17)
printf(“Eligible for voting”);
}

CM304.41TO42 10
PROGRAM DISPLAY
#include<stdio.h>
Enter age of
main()
candidate:
{
40
int age;
printf(“Enter age of Eligible for voting
candidate:\n”);
scanf(“%d”,&age); age
if(age > 17) 40
printf(“Eligible for voting”);
Memory Contents
}

CM304.41TO42 11
PROGRAM DISPLAY
#include<stdio.h>
Enter age of
main() candidate:
{
15
int age;
printf(“Enter age of
candidate:\n”); age
scanf(“%d”,&age);
15
if(age > 17)
Memory Contents
printf(“Eligible for voting”);
}
CM304.41TO42 12
To determine whether number entered through
keyboard is even or odd.

Algorithm
• Read the number
• If number is divisible by 2
• Then display “even”
• If number is not divisible by 2
• Then display “odd”

CM304.41TO42 13
PROGRAM
#include<stdio.h>
main()
{
int num;
printf(“Enter any number:\n”);
scanf(“%d”,&num);
if(num % 2 == 0)
printf(“Even”);
if(num% 2 != 0)
printf(“odd”);
}

CM304.41TO42 14
PROGRAM DISPLAY
#include<stdio.h> Enter any number:
main() 15
{
odd
int num;
printf(“Enter any number:\n”);
scanf(“%d”,&num);
num
if(num % 2 ==0)
printf(“Even”); 15
if(num% 2 != 0)
Memory Contents
printf(“odd”);
}

CM304.41TO42 15
PROGRAM DISPLAY
#include<stdio.h> Enter any number:
main() 18
{
Even
int num;
printf(“Enter any number:\n”);
scanf(“%d”,&num);
if(num % 2 == 0) num
printf(“Even”);
18
if(num% 2 != 0)
printf(“odd”); Memory Contents
}

CM304.41TO42 16
To find the largest among three numbers.
Algorithm
• Read three numbers a, b, c
• Place the value of a into 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.41TO42 17
Start Flowchart
Read a,b,c

largest  a

true
b>largest largest  b

false
true
c>largest largest  c

false
Print largest CM304.41TO42 18
PROGRAM
#include<stdio.h>
main()
{
int a, b, c, largest;
printf(“Enter a, b, c :\n”);
scanf(“%d%d%d”,&a, &b, &c);
largest = a;
if(b>largest)
largest = b;
if(c>largest)
largest = c;
printf(“largest is %d”, largest);
}

CM304.41TO42 19
PROGRAM DISPLAY
#include<stdio.h> Enter a, b, c :
main() 89 69 98
{
int a, b, c, largest;
printf(“Enter a, b, c :\n”);
scanf(“%d%d%d”,&a, &b, &c);
largest = a;
if(b>largest)
largest = b;
if(c>largest) a b c largest
largest = c;
89 69 98 89
printf(“largest is %d”, largest);
} Memory Contents
CM304.41TO42 20
PROGRAM DISPLAY
#include<stdio.h> Enter a, b, c :
main() 3 69 98
{ largest is 98
int a, b, c, largest;
printf(“Enter a, b, c :\n”);
scanf(“%d%d%d”,&a, &b, &c);
largest = a;
if(b>largest)
largest = b;
if(c>largest) a b c largest
largest = c;
89 69 98 98
printf(“largest is %d”, largest);
} Memory Contents
CM304.41TO42 21
PROGRAM DISPLAY
#include<stdio.h>
Enter a, b, c :
main()
{ 89 99 36
int a, b, c, largest;
printf(“Enter a, b, c :\n”);
scanf(“%d%d%d”,&a, &b, &c);
largest = a;
if(b>largest)
largest = b;
if(c>largest)
a b c largest
largest = c;
printf(“largest is %d”, largest); 89 99 36 89
}
Memory Contents
CM304.41TO42 22
PROGRAM DISPLAY

#include<stdio.h> Enter a, b, c :
main() 3 99 36
{ largest is 99
int a, b, c, largest;
printf(“Enter a, b, c :\n”);
scanf(“%d%d%d”,&a, &b, &c);
largest = a;
if(b>largest)
largest = b;
if(c>largest) a b c largest
largest = c;
89 99 36 99
printf(“largest is %d”, largest);
} Memory Contents
CM304.41TO42 23
IF .. ELSE Statement
• if.. else statement is used to execute one group of
the statements if condition is true or other group
of statements if condition is false.
• The syntax of if..else statement is
if (condition)
{
statements block1;
}
else
{
statements block2;
}
next statements;
CM304.41TO42 24
Flowchart of if Statement

Condition or
Statements Statements
False expression True

Next Statements
CM304.41TO42 25
To determine whether candidate is eligible for
voting or not.

Algorithm
• Read the age of the candidate
• If age is greater than 17
• Then display “eligible for voting”
• Otherwise display “not eligible for voting”

CM304.41TO42 26
PROGRAM
#include<stdio.h>
main()
{
int age;
printf(“Enter age of candidate:\n”);
scanf(“%d”,&age);
if(age > 17)
printf(“Eligible for voting”);
else
printf(“Not eligible for voting”);
}

CM304.41TO42 27
PROGRAM DISPLAY
#include<stdio.h> Enter age of candidate:
main() 15
{ Not eligible for voting
int age;
printf(“Enter age of candidate:\n”);
scanf(“%d”,&age);
if(age > 17) age

printf(“Eligible for voting”); 15


else
Memory Contents
printf(“Not eligible for voting”);
}

CM304.41TO42 28
PROGRAM DISPLAY

#include<stdio.h> Enter age of candidate:

main() 25

{ Eligible for voting

int age;
printf(“Enter age of candidate:\n”);
scanf(“%d”,&age);
if(age > 17) age
printf(“Eligible for voting”);
25
else
printf(“Not eligible for voting”); Memory Contents

CM304.41TO42 29
To determine whether number entered through
keyboard is even or odd

Algorithm
• Read the number
• If number is divisible by 2
• Then display “even”
• Otherwise display “odd”

CM304.41TO42 30
PROGRAM DISPLAY
#include<stdio.h> Enter any number:
main() 15
{ odd
int num;
printf(“Enter any number:\n”);
scanf(“%d”,&num);
if(num % 2 ==0) num
printf(“Even”);
15
else
printf(“odd”); Memory Contents

CM304.41TO42 31
PROGRAM DISPLAY
#include<stdio.h> Enter any number:
main() 8
{ Even
int num;
printf(“Enter any number:\n”);
scanf(“%d”,&num);
if(num % 2 ==0) num
printf(“Even”);
8
else
printf(“odd”); Memory Contents
}

CM304.41TO42 32
To determine whether number entered through
keyboard is Positive or Negative

Algorithm
• Read the number
• If number is less than 0
• Then display “Negative”
• Otherwise display “Positive”

CM304.41TO42 33
PROGRAM DISPLAY
#include<stdio.h> Enter any number:
main() 15
{ Positive
int num;
printf(“Enter any number:\n”);
scanf(“%d”,&num);
if(num < 0) num
printf(“Negative”);
15
else
printf(“Positive”); Memory Contents
}

CM304.41TO42 34
PROGRAM DISPLAY
Enter any number:
#include<stdio.h>
-18
main()
Negative
{
int num;
printf(“Enter any number:\n”);
scanf(“%d”,&num);
num
if(num < 0)
printf(“Negative”); -18
else
Memory Contents
printf(“positive”);
}
CM304.41TO42 35
Summary

In this class, we have learnt about

• Control Statements
• Branching Statements
• Conditional statements
• if Statement
• if .. Else statement

CM304.41TO42 36
QUIZ
1. In the following segment of program find the
values of x and y,
a) when value of n is 0
b) when value of n is 1.
x = 1;
y = 1;
if(n > 0)
x = x + 1;
y = y – 1;
printf(“%d %d”, x, y);
CM304.41TO42 37
Answers
a) x=1, y=0
b) x=2, y=0

CM304.41TO42 38
Frequently Asked Questions

1. Classify the Control Statements in C?

3. Explain the use of if and if..else Statements with


examples?

5. Write a C program to find the greatest among three


numbers using if Statement?

CM304.41TO42 39

You might also like