SlideShare uma empresa Scribd logo
1 de 19
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.

Mais conteúdo relacionado

Mais procurados

Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and commentsNilimesh Halder
 
Conditionalstatement
ConditionalstatementConditionalstatement
ConditionalstatementRaginiJain21
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonPriyankaC44
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception HandlingMegha V
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS abhishek kumar
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming LanguageExplore Skilled
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and PackagesDamian T. Gordon
 
Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaEdureka!
 

Mais procurados (20)

Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Conditionalstatement
ConditionalstatementConditionalstatement
Conditionalstatement
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Array in c
Array in cArray in c
Array in c
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming Language
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Structure in C
Structure in CStructure in C
Structure in C
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | Edureka
 

Semelhante a Conditional and control statement

conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfconditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfsdvdsvsdvsvds
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONvikram mahendra
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statementsrajshreemuthiah
 
if else python.pdf
if else python.pdfif else python.pdf
if else python.pdfGshs6
 
controlstatementspy.docx
controlstatementspy.docxcontrolstatementspy.docx
controlstatementspy.docxmanohar25689
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsDhivyaSubramaniyam
 
05. Conditional Statements
05. Conditional Statements05. Conditional Statements
05. Conditional StatementsIntro C# Book
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c languagechintupro9
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfDheeravathBinduMadha
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdfNehaSpillai1
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)jahanullah
 

Semelhante a Conditional and control statement (20)

conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdfconditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
 
loops.pdf
loops.pdfloops.pdf
loops.pdf
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
 
if else python.pdf
if else python.pdfif else python.pdf
if else python.pdf
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
controlstatementspy.docx
controlstatementspy.docxcontrolstatementspy.docx
controlstatementspy.docx
 
Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
 
05. Conditional Statements
05. Conditional Statements05. Conditional Statements
05. Conditional Statements
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Cin and cout
Cin and coutCin and cout
Cin and cout
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 

Último

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersMairaAshraf6
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 

Último (20)

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 

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.