SlideShare a Scribd company logo
3
Most read
9
Most read
11
Most read
CONDITIONAL STATEMENTS
and
CONTROL STATEMENTS
1
Prepared by
V.Kumararaja, AP/IT
S.Thanga Prasath, AP/CSE
Er.Perumal Manimekalai Colleg of Engineering,
Hosur
CONDITIONAL STATEMENTS (Decision Making)
The basic decision statements in computer is
selection structure.
The decision is described to computer as a
conditional statement that can be answered
True or False.
Python language provide the following
conditional (Decision making) statements.
2
if statement
if...else statement
if...elif...else staement
Nested if..else statement
The if statement
The if statement is a decision making statement.
It is used to control the flow of execution of the
statements and also used to test logically
whether the condition is true or false.
Syntax
3
if test expression:
statement(s)
Example program
i=int(input(“Enter the number:”))
If (i<=10):
print(“ condition is true”)
4
OUTPUT
Enter the number: 9
Condition is true
If … else statement
The if…else statement is called alternative
execution, in which there are two possibilities
and the condition determines wich one gets
executed.
Syntax
5
if test expression:
Body of if
else:
Body of else
Write a program to check if a number is Odd or Even
num = int(input(“Enter the number:”))
if (num % 2)== 0:
print (“Given number is Even”)
else:
print(“ Given number is Odd”)
6
OUTPUT
Enter the number: 9
Given number is Odd
elif Statements
 elif – is a keyword used in Python in
replacement of else if to place another condition
in the program. This is called chained conditional.
 Chained conditions allows than two
possibilities and need more than two branches.
SYNTAX
7
if expression:
Body of if
elif expression:
Body of elif
else:
Body of else
Figure – elif condition Flowchart
8
Example: largest among three numbers
a = int(input(“Enter 1st number:”))
b= int(input(“Enter 2nd number:”))
c= int(input(“Enter 3rd number:”))
if (a > b) and (a > c):
print("a is greater")
elif (b < a) and (b < c):
print(“b is greater")
else:
print(“c is greater")
9
OUTPUT
Enter 1st number:10
Enter 2nd number:25
Enter 3rd number:15
B is greater
Nested if … else Statements
 We can write an entire if… else statement in
another if… else statement called nesting, and the
statement is called nested if.
 In a nested if construct, you can have an if … elif
… else construct inside an if … elif.. Else construct.
10
Syntax
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
.
11
• Example program
n = int(input(“Enter number:”))
If (n<=15):
if (n == 10):
print(‘play cricket’)
else:
print(‘play kabadi’)
Else:
print(‘Don’t play game’)
12
OUTPUT
Enter number : 10
Play cricket
CONTROL STATEMENT (Looping Statement)
Program statement are executed sequentially
one after another. In some situations, a block
of code needs of times.
These are repetitive program codes, the
computers have to perform to complete tasks.
The following are the loop structures available
in python.
13
while statement
for loop statement
Nested loop staement
While loop statement
 A while loop statement in Python
programming language repeatedly executes a
target statement as long as a given condition is
true.
Syntax of while loop
14
while expression:
statement(s)
Write a program to find sum of number
num = int(input("Enter a number: "))
sum = 0
while(num > 0):
sum = sum+num
num = num-1
print("The sum is",sum)
15
OUTPUT
Enter a number: 10
The sum is 55
Using else statement with while loops
 Python supports t have an else statement associated
with a loop statement.
 If the else statement is used with a while loop, the
else statement is executed when the condition false.
Program to illustrate the else in while loop
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print(“Outside loop")
16
OUTPUT
Inside loop
Inside loop
Inside loop
Outside loop
For loop statement
The for loop is another repetitive control
structure, and is used to execute a set of
instructions repeatedly, until the condition
becomes false.
The for loop in Python is used to iterate over a
sequence (list, tuple, string) or other iterable
objects. Iterating over a sequence is called
traversal.
Syntax
17
for val in sequence:
Body of for loop
For loop flow chart
18
Addition of number using for loop
numbers = [6, 5, 3, 8, 4, 2, 5, 4]
sum1 = 0
for val in numbers:
sum1 = sum1+val
print("The sum is", sum1)
OUTPUT
The sum is 37
for Loop and for Loop with else
EX-01:
genre = ['pop', 'rock', 'jazz']
for i in range(len(genre)):
print("I like", genre[i])
EX-02:
genre = ['pop', 'rock', 'jazz']
for i in range(len(genre)):
print("I like", genre[i])
else:
print("No items left.")
OUTPUT
I like pop
I like rock ​
I like jazz
OUTPUT
I like pop
I like rock ​
I like jazz
No items left.

More Related Content

What's hot (20)

PPTX
Data types in python
RaginiJain21
 
PDF
Python basic
Saifuddin Kaijar
 
PDF
Operators in python
Prabhakaran V M
 
PPTX
Presentation on python
william john
 
PPT
FUNCTIONS IN c++ PPT
03062679929
 
PDF
Introduction to Python
Mohammed Sikander
 
PPTX
Python - An Introduction
Swarit Wadhe
 
PPTX
Looping Statements and Control Statements in Python
PriyankaC44
 
PPTX
Functions in python slide share
Devashish Kumar
 
PDF
Python introduction
Jignesh Kariya
 
PPTX
Introduction to Basics of Python
Elewayte
 
ODP
Python Modules
Nitin Reddy Katkam
 
PDF
Datatypes in python
eShikshak
 
PPTX
oops concept in java | object oriented programming in java
CPD INDIA
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PPTX
Control structures in java
VINOTH R
 
PDF
Python exception handling
Mohammed Sikander
 
PPTX
Method overloading
Lovely Professional University
 
DOC
Time and space complexity
Ankit Katiyar
 
Data types in python
RaginiJain21
 
Python basic
Saifuddin Kaijar
 
Operators in python
Prabhakaran V M
 
Presentation on python
william john
 
FUNCTIONS IN c++ PPT
03062679929
 
Introduction to Python
Mohammed Sikander
 
Python - An Introduction
Swarit Wadhe
 
Looping Statements and Control Statements in Python
PriyankaC44
 
Functions in python slide share
Devashish Kumar
 
Python introduction
Jignesh Kariya
 
Introduction to Basics of Python
Elewayte
 
Python Modules
Nitin Reddy Katkam
 
Datatypes in python
eShikshak
 
oops concept in java | object oriented programming in java
CPD INDIA
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Control structures in java
VINOTH R
 
Python exception handling
Mohammed Sikander
 
Method overloading
Lovely Professional University
 
Time and space complexity
Ankit Katiyar
 

Similar to Conditional and control statement (20)

PDF
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
sdvdsvsdvsvds
 
PDF
loops.pdf
AmayJaiswal4
 
PPTX
industry coding practice unit-2 ppt.pptx
LakshmiMarineni
 
PPTX
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
PPTX
ControlStructures.pptx5t54t54444444444444444
pawankamal3
 
PDF
basic of desicion control statement in python
nitamhaske
 
PPTX
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 
PPTX
Conditional Statements.pptx
Gautam623648
 
PDF
E-Notes_3721_Content_Document_20250107032537PM.pdf
aayushihirpara297
 
PDF
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
 
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
PPSX
class interview demo
HELP4STUDENTS
 
PPTX
Demo for Class.pptx
HELP4STUDENTS
 
PPTX
python
HELP4STUDENTS
 
PPTX
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
PPTX
pds first unit module 2 MODULE FOR ppt.pptx
bmit1
 
PPTX
python.pptx
Poornima116356
 
PDF
Python Decision Making And Loops.pdf
NehaSpillai1
 
PPTX
1. control structures in the python.pptx
DURAIMURUGANM2
 
PPTX
Control_Statements.pptx
Koteswari Kasireddy
 
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
sdvdsvsdvsvds
 
loops.pdf
AmayJaiswal4
 
industry coding practice unit-2 ppt.pptx
LakshmiMarineni
 
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
ControlStructures.pptx5t54t54444444444444444
pawankamal3
 
basic of desicion control statement in python
nitamhaske
 
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 
Conditional Statements.pptx
Gautam623648
 
E-Notes_3721_Content_Document_20250107032537PM.pdf
aayushihirpara297
 
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
 
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
class interview demo
HELP4STUDENTS
 
Demo for Class.pptx
HELP4STUDENTS
 
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
pds first unit module 2 MODULE FOR ppt.pptx
bmit1
 
python.pptx
Poornima116356
 
Python Decision Making And Loops.pdf
NehaSpillai1
 
1. control structures in the python.pptx
DURAIMURUGANM2
 
Control_Statements.pptx
Koteswari Kasireddy
 
Ad

Recently uploaded (20)

DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
MRRS Strength and Durability of Concrete
CivilMythili
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Hashing Introduction , hash functions and techniques
sailajam21
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Ad

Conditional and control statement

  • 1. CONDITIONAL STATEMENTS and CONTROL STATEMENTS 1 Prepared by V.Kumararaja, AP/IT S.Thanga Prasath, AP/CSE Er.Perumal Manimekalai Colleg of Engineering, Hosur
  • 2. CONDITIONAL STATEMENTS (Decision Making) The basic decision statements in computer is selection structure. The decision is described to computer as a conditional statement that can be answered True or False. Python language provide the following conditional (Decision making) statements. 2 if statement if...else statement if...elif...else staement Nested if..else statement
  • 3. The if statement The if statement is a decision making statement. It is used to control the flow of execution of the statements and also used to test logically whether the condition is true or false. Syntax 3 if test expression: statement(s)
  • 4. Example program i=int(input(“Enter the number:”)) If (i<=10): print(“ condition is true”) 4 OUTPUT Enter the number: 9 Condition is true
  • 5. If … else statement The if…else statement is called alternative execution, in which there are two possibilities and the condition determines wich one gets executed. Syntax 5 if test expression: Body of if else: Body of else
  • 6. Write a program to check if a number is Odd or Even num = int(input(“Enter the number:”)) if (num % 2)== 0: print (“Given number is Even”) else: print(“ Given number is Odd”) 6 OUTPUT Enter the number: 9 Given number is Odd
  • 7. elif Statements  elif – is a keyword used in Python in replacement of else if to place another condition in the program. This is called chained conditional.  Chained conditions allows than two possibilities and need more than two branches. SYNTAX 7 if expression: Body of if elif expression: Body of elif else: Body of else
  • 8. Figure – elif condition Flowchart 8
  • 9. Example: largest among three numbers a = int(input(“Enter 1st number:”)) b= int(input(“Enter 2nd number:”)) c= int(input(“Enter 3rd number:”)) if (a > b) and (a > c): print("a is greater") elif (b < a) and (b < c): print(“b is greater") else: print(“c is greater") 9 OUTPUT Enter 1st number:10 Enter 2nd number:25 Enter 3rd number:15 B is greater
  • 10. Nested if … else Statements  We can write an entire if… else statement in another if… else statement called nesting, and the statement is called nested if.  In a nested if construct, you can have an if … elif … else construct inside an if … elif.. Else construct. 10
  • 11. Syntax if expression1: statement(s) if expression2: statement(s) elif expression3: statement(s) else: statement(s) . 11
  • 12. • Example program n = int(input(“Enter number:”)) If (n<=15): if (n == 10): print(‘play cricket’) else: print(‘play kabadi’) Else: print(‘Don’t play game’) 12 OUTPUT Enter number : 10 Play cricket
  • 13. CONTROL STATEMENT (Looping Statement) Program statement are executed sequentially one after another. In some situations, a block of code needs of times. These are repetitive program codes, the computers have to perform to complete tasks. The following are the loop structures available in python. 13 while statement for loop statement Nested loop staement
  • 14. While loop statement  A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true. Syntax of while loop 14 while expression: statement(s)
  • 15. Write a program to find sum of number num = int(input("Enter a number: ")) sum = 0 while(num > 0): sum = sum+num num = num-1 print("The sum is",sum) 15 OUTPUT Enter a number: 10 The sum is 55
  • 16. Using else statement with while loops  Python supports t have an else statement associated with a loop statement.  If the else statement is used with a while loop, the else statement is executed when the condition false. Program to illustrate the else in while loop counter = 0 while counter < 3: print("Inside loop") counter = counter + 1 else: print(“Outside loop") 16 OUTPUT Inside loop Inside loop Inside loop Outside loop
  • 17. For loop statement The for loop is another repetitive control structure, and is used to execute a set of instructions repeatedly, until the condition becomes false. The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal. Syntax 17 for val in sequence: Body of for loop
  • 18. For loop flow chart 18 Addition of number using for loop numbers = [6, 5, 3, 8, 4, 2, 5, 4] sum1 = 0 for val in numbers: sum1 = sum1+val print("The sum is", sum1) OUTPUT The sum is 37
  • 19. for Loop and for Loop with else EX-01: genre = ['pop', 'rock', 'jazz'] for i in range(len(genre)): print("I like", genre[i]) EX-02: genre = ['pop', 'rock', 'jazz'] for i in range(len(genre)): print("I like", genre[i]) else: print("No items left.") OUTPUT I like pop I like rock ​ I like jazz OUTPUT I like pop I like rock ​ I like jazz No items left.