SlideShare une entreprise Scribd logo
1  sur  86
CHAPTER - IV
CONDITIONAL AND ITERATIVE
STATEMENTS
CHAPTER - IV
CONDITIONAL AND ITERATIVE
STATEMENTS
SELECTION ITERATION
CONDITIONAL AND ITERATIVE
STATEMENTS
INTRODUCTION
INTRODUCTION
Programs are written for the solution to
the real world problems. A language should
have the ability to control the flow of execution
so that at different intervals different
statements can be executed. Structured
programming is a paradigm aims at controlling
the flow of execution of statements in a
program by using control structures.
A language which supports the control
structures is called as structured programming
language
TYPES OF CONTROL STRUCTURES
A Structured programming is an important
feature of a programming language which
comprises following logical structure:
1. SEQUENCE
2. SELECTION
3. ITERATION OR LOOPING
4. BRANCHING OR JUMPING STATEMENTS
TYPES OF CONTROL STRUCTURES
Sequence is the default control structure;
instructions are executed one after another.
Statement 1
Statement 2
Statement 3
……..
……..
……..
1. SEQUENCE
1. SEQUENCE – FLOW CHART
1. SEQUENCE – FLOW CHART
Statement 1
Statement 2
Statement 3
1. SEQUENCE - PROGRAM
Sequence is the default control structure;
instructions are executed one after another.
# This program adds two numbers
def sum_of_two_no():
num1 = 1.5
num2 = 6.3
sum = float(num1) + float(num2)
print('The sum is =‘, sum)
sum_of_two_no():
1. SEQUENCE - PROGRAM
2. SELECTION
SELECTION
A selection statement causes the
program control to be transferred to a
specific flow based upon whether a certain
condition is true or not.
2. SELECTION
CONDITIONAL CONSTRUCT – if else STATEMENT
CONDITIONAL CONSTRUCT – if else STATEMENT
Conditional constructs (also known as if
statements) provide a way to execute a chosen
block of code based on the run-time evaluation of
one or more Boolean expressions. In Python, the
most general form of a conditional is written as
follows:
Contd.. Next Slide
CONDITIONAL CONSTRUCT – if else STATEMENT
if first condition:
first body
elif second condition:
second body
elif third condition:
third body
else:
fourth body
: Colon Must
CONDITIONAL CONSTRUCT – if else STATEMENT
FLOW CHART
CONDITIONAL CONSTRUCT – if else STATEMENT
FLOW CHART
Condition ? Statement 1 Statement 2
Statement 1
Statement 2
False
True
else
body
Main
Body
CONDITIONAL CONSTRUCT – if else STATEMENT
Each condition is a Boolean expression, and
each body contains one or more commands that
are to be executed conditionally.
If the first condition succeeds, the first body will
be executed; no other conditions or bodies are
evaluated in that case.
CONDITIONAL CONSTRUCT – if else STATEMENT
If the first condition fails, then the process
continues in similar manner with the evaluation
of the second condition. The execution of this
overall construct will cause precisely one of the
bodies to be executed.
There may be any number of elif clauses
(including zero), and
 The final else clause is optional.
CONDITIONAL CONSTRUCT – if else STATEMENT
EXAMPLE - PROGRAM
EXAMPLES – if STATEMENT
OUT PUT
else is missing,
it is an optional
statement
CONDITIONAL CONSTRUCT
EXAMPLE – if else STATEMENT
EXAMPLE – if else STATEMENT
OUT PUT
else is
used
: Colon Must
CONDITIONAL CONSTRUCT
EXAMPLES – if elif STATEMENT
EXAMPLES – if elif STATEMENT
READ AS
18 is less
than age
and
18 is less
than 60
OUTPUT
PROGRAM LIST ON if CONTSTUCT
PROGRAM LIST ON if CONTSTUCT
1. Write a PYTHON program that reads a value of
n and check the number is zero or non zero value.
2. Write a PYTHON program to find a largest of
two numbers.
3. Write a PYTHON program that reads the
number and check the no is positive or negative.
4. Write a PYTHON program to check entered
character is vowel or consonant.
BELOW AVERAGE PROGRAMS
PROGRAM LIST ON if CONTSTUCT
5. Write a PYTHON program to evaluate the
student performance
If % is >=90 then Excellent performance
If % is >=80 then Very Good performance
If % is >=70 then Good performance
If % is >=60 then average performance
else Poor performance.
6. Write a PYTHON program to find largest of
three numbers.
7. Write a PYTHON program to find smallest of
three numbers
AVERAGE PROGRAMS
PROGRAM LIST ON if CONTSTUCT
8.Write a PYTHON program to check weather
number is even or odd.
9.Write a PYTHON program to check a year for
leap year.
10. A company insures its drivers in the following
cases:
- If the driver is married.
- If the driver is unmarried, male and above 30
years of age.
- If the driver is unmarried, female and above 25
years of age.
ABOVE AVERAGE PROGRAMS
PROGRAM LIST ON if CONTSTUCT
In all the other cases, the driver is not insured.
If the marital status, sex and age of the driver are
the inputs,
Write a PYTHON program to determine
whether the driver is insured or not
***
ABOVE AVERAGE PROGRAMS
3. ITERATION OR LOOPING
ITERATION
3. ITERATION OR LOOPING
What is loop or iteration?
Loops can execute a block of code number
of times until a certain condition is met.
OR
The iteration statement allows instructions to
be executed until a certain condition is to be
fulfilled.
The iteration statements are also called as
loops or Looping statements.
3. ITERATION OR LOOPING
Python provides two kinds of loops &
they are,
for loop
while loop
while loop
while loop
A while loop allows general repetition
based upon the repeated testing of a Boolean
condition
The syntax for a while loop in Python is as
follows:
while condition:
body
Where, loop body contain the single
statement or set of statements (compound
statement) or an empty statement.
Contd..
: Colon Must
while loop
The loop iterates while the expression
evaluates to true, when expression becomes
false the loop terminates.
while loop
FLOW CHART
while loop – Programming example
while loop - programs
OUTPUT
# Natural Numbers generation
while loop - programs
OUTPUT
# Calculating Sum of Natural Numbers
while loop - programs
#Generating Fibonacci numbers
while loop - programs
#Generating Fibonacci numbers
OUTPUT
while loop - Programs
Class work / Home Work
while loop - programs
1. Write a PYTHON program to print the natural
numbers up to n
2. Write a PYTHON program to print even
numbers up to n
3. Write a PYTHON program to print odd
numbers up to n
4. Write a PYTHON program to print sum of
natural numbers up to n
BELOW AVERAGE PROGRAMS
while loop - programs
5. Write a PYTHON program to print sum of odd
numbers up to n
6. Write a PYTHON program to print sum of even
numbers up to n
7. Write a PYTHON program to print natural
numbers up to n in reverse order.
8. Write a PYTHON program to print Fibonacci
series up to n
9. Write a PYTHON program find a factorial of
given number
AVERAGE PROGRAMS
while loop - programs
10. Write a PYTHON program to check the
entered number is prime or not
11. Write a PYTHON program to find the sum of
digits of given number
12. Write a PYTHON program to check the
entered number is palindrome or not
13. Write a PYTHON program to reverse the given
number.
ABOVE AVERAGE & HOTS PROGRAMS
while loop - programs
14. Write a PYTHON program to print the
multiplication table
15. Write a PYTHON program to print the largest
of n numbers
16. Write a PYTHON program to print smallest of
n numbers
ABOVE AVERAGE & HOTS PROGRAMS
for LOOP
for LOOP
Python’s for-loop syntax is a more
convenient alternative to a while loop when
iterating through a series of elements. The for-
loop syntax can be used on any type of iterable
structure, such as a list, tuple str, set, dict, or
file
Syntax or general format of for loop is,
for element in iterable:
body
for LOOP
Python’s for-loop syntax is a more
convenient alternative to a while loop when
iterating through a series of elements. The for-
loop syntax can be used on any type of iterable
structure, such as a list, tuple str, set, dict, or
file
Syntax or general format of for loop is,
for element in iterable:
body
for LOOP
OUTPUT
Till the list
exhaust for loop
will continue to
execute.
for LOOP
range KEYWORD
for LOOP - range KEYWORD
The range() function returns a
sequence of numbers, starting from 0 by
default, and increments by 1 (by default),
and ends at a specified number.
range(start, stop, step)
for n in range(3,6):
print(n)
x = range(3, 6)
for n in x:
print(n)
OR
for LOOP - range KEYWORD
OUTPUT
#Generating series of numbers
for LOOP - range KEYWORD
OUTPUT
#Generating even numbers
for LOOP – len() FUNCTION
for LOOP - range KEYWORD
# print string character by character
OUTPUT
else statement in loop
else statement in loop
else can be used in for and while loops
the else body will be executed as and when the
loop’s conditional expression evaluates to false
OUTPUT
for loop - Programs
Class work / Home Work
for loop – Programs - Class work / Home Work
1. Write a PYTHON program to print the natural
numbers up to n
2. Write a PYTHON program to print even numbers
up to n
3. Write a PYTHON program to print odd numbers
up to n
4. Write a PYTHON program that prints 1 2 4 8
16 32 … n2
5. Write a PYTHON program to sum the given
sequence
1 + 1/ 1! + 1/ 2! + 1/3! + …. + 1/n!
BELLOW AVERAGE PROGRAMS
for loop – Programs - Class work / Home Work
6. Write a PYTHON program to compute the cosine
series
cos(x) = 1 – x2 / 2! + x4 / 4! – x6 / 6! + … xn / n!
7. Write a short PYTHON program to check weather
the square root of number is prime or not.
8. Write a PYTHON program to produce following
design
A B C
A B C
A B C
AVERAGE PROGRAMS
for loop – Programs - Class work / Home Work
9. Write a PYTHON program to produce following
design
A
A B
A B C
A B C D
A B C D E
If user enters n value as 5
ABOVE AVERAGE PROGRAMS
for loop – Programs - Class work / Home Work
10. Write a PYTHON program to produce following
design
A B C D E
A B C D
A B C
A B
A
(If user enters n value as 5)
ABOVE AVERAGE PROGRAMS
for loop – Programs - Class work / Home Work
11. Write a PYTHON program to produce
following design
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
If user enters n value as 5
ABOVE AVERAGE PROGRAMS
for loop – Programs - Class work / Home Work
12. Write a PYTHON program to produce
following design
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
If user enters n value as 5
ABOVE AVERAGE PROGRAMS
4. BRANCHING OR JUMPING STATEMENTS
4. BRANCHING OR JUMPING STATEMENTS
Python has an unconditional branching
statements and they are,
1. break STATEMENT
2. continue STATEMENT
4. BRANCHING OR JUMPING STATEMENTS
1. break STATEMENT
Break can be used to unconditionally
jump out of the loop. It terminates the
execution of the loop. Break can be used in
while loop and for loop. Break is mostly
required, when because of some external
condition, we need to exit from a loop.
1. break STATEMENT
break
causes
jump
Condition ?
Statement 1
break
Loop
True
1. break STATEMENT
OUT PUT
2. continue STATEMENT
The continue statement in
Python returns the control to the
beginning of the while loop. The continue
statement rejects all the
remaining statements in the current
iteration of the loop and moves the
control back to the top of the loop.
The continue statement can be used in
both while and for loops.
2. continue STATEMENT
continue
causes
jump
Condition ?
Statement 1
continue
Loop
True
Statement n
Statements
ignored or
skipped
False
2. continue STATEMENT
when i value becomes 2 the print statement gets
skipped, continue statement goes for next iteration,
hence in the out put 2 is not printed
pass STATEMENT
pass STATEMENT
The pass statement in Python is used when a
statement is required syntactically but you do not
want any command or code to execute.
The pass statement is a null operation; nothing
happens when it executes.
The pass is also useful in places where your
code will eventually go, but has not been written
yet (e.g., in stubs for example):
pass STATEMENT
pass in loop
pass in loop has
no output
Difference Between break and continue
break continue
Difference Between break and continue
BREAK CONTINUE
It terminates the execution
of remaining iteration of
the loop.
It terminates only the current
iteration of the loop.
'break' resumes the control
of the program to the end
of loop enclosing that
'break'.
'continue' resumes the control
of the program to the next
iteration of that loop enclosing
'continue'.
It causes early termination
of loop.
It causes early execution of
the next iteration.
'break' stops the
continuation of loop.
'continue' do not stops the
continuation of loop, it only
stops the current iteration.
CLASS TEST
CLASS TEST
Time: 40 Min Max Marks: 20
1. Explain the types control structures 05
2. Write a program to check number is prime or
not 05
3. Write a program to check the entered number
is Armstrong number or not 05
4. Differentiate between break and continue 05
Thank You

Contenu connexe

Tendances

Sejarah perkembangan komputer
Sejarah perkembangan komputerSejarah perkembangan komputer
Sejarah perkembangan komputer
Demi Yurfina
 
Entrepreneurship development through vocational education
Entrepreneurship development through vocational education Entrepreneurship development through vocational education
Entrepreneurship development through vocational education
Alexander Decker
 
Power Point Presentasi Komunikasi Data
Power Point Presentasi Komunikasi DataPower Point Presentasi Komunikasi Data
Power Point Presentasi Komunikasi Data
dodolbetawi
 
Optical Computing Technology
Optical Computing TechnologyOptical Computing Technology
Optical Computing Technology
Kanchan Shinde
 

Tendances (20)

Security and privacy issues with mobile cloud computing applications june 2016
Security and privacy issues with mobile cloud computing applications june 2016Security and privacy issues with mobile cloud computing applications june 2016
Security and privacy issues with mobile cloud computing applications june 2016
 
1 pengenalan-konsep-imk
1 pengenalan-konsep-imk1 pengenalan-konsep-imk
1 pengenalan-konsep-imk
 
Diamond chip
Diamond chipDiamond chip
Diamond chip
 
Impact of it boon or bane
Impact of it  boon or baneImpact of it  boon or bane
Impact of it boon or bane
 
Information technology boon to society
Information technology boon to societyInformation technology boon to society
Information technology boon to society
 
Diamond chip
Diamond chipDiamond chip
Diamond chip
 
Diamond Chip Technology
Diamond Chip TechnologyDiamond Chip Technology
Diamond Chip Technology
 
Bab 5 - Jaringan Komputer dan Internet
Bab 5 - Jaringan Komputer dan InternetBab 5 - Jaringan Komputer dan Internet
Bab 5 - Jaringan Komputer dan Internet
 
Sejarah perkembangan komputer
Sejarah perkembangan komputerSejarah perkembangan komputer
Sejarah perkembangan komputer
 
Entrepreneurship development through vocational education
Entrepreneurship development through vocational education Entrepreneurship development through vocational education
Entrepreneurship development through vocational education
 
The latest trends in technology
The latest trends in technologyThe latest trends in technology
The latest trends in technology
 
Kriptografi, Enkripsi dan Dekripsi
Kriptografi, Enkripsi dan DekripsiKriptografi, Enkripsi dan Dekripsi
Kriptografi, Enkripsi dan Dekripsi
 
Group seminar report on cloud computing
Group seminar report on cloud computingGroup seminar report on cloud computing
Group seminar report on cloud computing
 
negative aspects of I.T.
negative aspects of I.T.negative aspects of I.T.
negative aspects of I.T.
 
Seminar ppt-nanotechnology
Seminar ppt-nanotechnologySeminar ppt-nanotechnology
Seminar ppt-nanotechnology
 
Power Point Presentasi Komunikasi Data
Power Point Presentasi Komunikasi DataPower Point Presentasi Komunikasi Data
Power Point Presentasi Komunikasi Data
 
Optical Computing Technology
Optical Computing TechnologyOptical Computing Technology
Optical Computing Technology
 
Grid computing ppt
Grid computing pptGrid computing ppt
Grid computing ppt
 
Seminar report on blue eyes
Seminar report on blue eyesSeminar report on blue eyes
Seminar report on blue eyes
 
Future trends in technology
Future trends in technologyFuture trends in technology
Future trends in technology
 

Similaire à Chapter 9 Conditional and Iterative Statements.pptx

Chapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptxChapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptx
XhelalSpahiu
 
Fundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptxFundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptx
Eyasu46
 
265 ge8151 problem solving and python programming - 2 marks with answers
265   ge8151 problem solving and python programming - 2 marks with answers265   ge8151 problem solving and python programming - 2 marks with answers
265 ge8151 problem solving and python programming - 2 marks with answers
vithyanila
 
Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01
Getachew Ganfur
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptx
adihartanto7
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
thuhiendtk4
 

Similaire à Chapter 9 Conditional and Iterative Statements.pptx (20)

Chapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptxChapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptx
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
 
Fundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptxFundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptx
 
python.pptx
python.pptxpython.pptx
python.pptx
 
265 ge8151 problem solving and python programming - 2 marks with answers
265   ge8151 problem solving and python programming - 2 marks with answers265   ge8151 problem solving and python programming - 2 marks with answers
265 ge8151 problem solving and python programming - 2 marks with answers
 
complete data structure
complete data structurecomplete data structure
complete data structure
 
Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01
 
programming.ppt
programming.pptprogramming.ppt
programming.ppt
 
PPT_203105211_3.pptx
PPT_203105211_3.pptxPPT_203105211_3.pptx
PPT_203105211_3.pptx
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
 
Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
 
INTERNSHIP REPORT.docx
 INTERNSHIP REPORT.docx INTERNSHIP REPORT.docx
INTERNSHIP REPORT.docx
 
Learn Programming with Livecoding.tv http://goo.gl/tIgO1I
Learn Programming with Livecoding.tv http://goo.gl/tIgO1ILearn Programming with Livecoding.tv http://goo.gl/tIgO1I
Learn Programming with Livecoding.tv http://goo.gl/tIgO1I
 
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
 
C Language Presentation.pptx
C Language Presentation.pptxC Language Presentation.pptx
C Language Presentation.pptx
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
 
banaz hilmy.pptx
banaz hilmy.pptxbanaz hilmy.pptx
banaz hilmy.pptx
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptx
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 

Dernier

Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
amitlee9823
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
amitlee9823
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
amitlee9823
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
amitlee9823
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
amitlee9823
 

Dernier (20)

Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
ELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptxELKO dropshipping via API with DroFx.pptx
ELKO dropshipping via API with DroFx.pptx
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 

Chapter 9 Conditional and Iterative Statements.pptx

  • 1. CHAPTER - IV CONDITIONAL AND ITERATIVE STATEMENTS
  • 2. CHAPTER - IV CONDITIONAL AND ITERATIVE STATEMENTS SELECTION ITERATION
  • 5. INTRODUCTION Programs are written for the solution to the real world problems. A language should have the ability to control the flow of execution so that at different intervals different statements can be executed. Structured programming is a paradigm aims at controlling the flow of execution of statements in a program by using control structures. A language which supports the control structures is called as structured programming language
  • 6. TYPES OF CONTROL STRUCTURES
  • 7. A Structured programming is an important feature of a programming language which comprises following logical structure: 1. SEQUENCE 2. SELECTION 3. ITERATION OR LOOPING 4. BRANCHING OR JUMPING STATEMENTS TYPES OF CONTROL STRUCTURES
  • 8. Sequence is the default control structure; instructions are executed one after another. Statement 1 Statement 2 Statement 3 …….. …….. …….. 1. SEQUENCE
  • 9. 1. SEQUENCE – FLOW CHART
  • 10. 1. SEQUENCE – FLOW CHART Statement 1 Statement 2 Statement 3
  • 11. 1. SEQUENCE - PROGRAM
  • 12. Sequence is the default control structure; instructions are executed one after another. # This program adds two numbers def sum_of_two_no(): num1 = 1.5 num2 = 6.3 sum = float(num1) + float(num2) print('The sum is =‘, sum) sum_of_two_no(): 1. SEQUENCE - PROGRAM
  • 13.
  • 15. A selection statement causes the program control to be transferred to a specific flow based upon whether a certain condition is true or not. 2. SELECTION
  • 16. CONDITIONAL CONSTRUCT – if else STATEMENT
  • 17. CONDITIONAL CONSTRUCT – if else STATEMENT Conditional constructs (also known as if statements) provide a way to execute a chosen block of code based on the run-time evaluation of one or more Boolean expressions. In Python, the most general form of a conditional is written as follows: Contd.. Next Slide
  • 18. CONDITIONAL CONSTRUCT – if else STATEMENT if first condition: first body elif second condition: second body elif third condition: third body else: fourth body : Colon Must
  • 19. CONDITIONAL CONSTRUCT – if else STATEMENT FLOW CHART
  • 20. CONDITIONAL CONSTRUCT – if else STATEMENT FLOW CHART Condition ? Statement 1 Statement 2 Statement 1 Statement 2 False True else body Main Body
  • 21. CONDITIONAL CONSTRUCT – if else STATEMENT Each condition is a Boolean expression, and each body contains one or more commands that are to be executed conditionally. If the first condition succeeds, the first body will be executed; no other conditions or bodies are evaluated in that case.
  • 22. CONDITIONAL CONSTRUCT – if else STATEMENT If the first condition fails, then the process continues in similar manner with the evaluation of the second condition. The execution of this overall construct will cause precisely one of the bodies to be executed. There may be any number of elif clauses (including zero), and  The final else clause is optional.
  • 23. CONDITIONAL CONSTRUCT – if else STATEMENT EXAMPLE - PROGRAM
  • 24. EXAMPLES – if STATEMENT OUT PUT else is missing, it is an optional statement
  • 26. EXAMPLE – if else STATEMENT OUT PUT else is used : Colon Must
  • 28. EXAMPLES – if elif STATEMENT READ AS 18 is less than age and 18 is less than 60 OUTPUT
  • 29. PROGRAM LIST ON if CONTSTUCT
  • 30. PROGRAM LIST ON if CONTSTUCT 1. Write a PYTHON program that reads a value of n and check the number is zero or non zero value. 2. Write a PYTHON program to find a largest of two numbers. 3. Write a PYTHON program that reads the number and check the no is positive or negative. 4. Write a PYTHON program to check entered character is vowel or consonant. BELOW AVERAGE PROGRAMS
  • 31.
  • 32.
  • 33. PROGRAM LIST ON if CONTSTUCT 5. Write a PYTHON program to evaluate the student performance If % is >=90 then Excellent performance If % is >=80 then Very Good performance If % is >=70 then Good performance If % is >=60 then average performance else Poor performance. 6. Write a PYTHON program to find largest of three numbers. 7. Write a PYTHON program to find smallest of three numbers AVERAGE PROGRAMS
  • 34. PROGRAM LIST ON if CONTSTUCT 8.Write a PYTHON program to check weather number is even or odd. 9.Write a PYTHON program to check a year for leap year. 10. A company insures its drivers in the following cases: - If the driver is married. - If the driver is unmarried, male and above 30 years of age. - If the driver is unmarried, female and above 25 years of age. ABOVE AVERAGE PROGRAMS
  • 35. PROGRAM LIST ON if CONTSTUCT In all the other cases, the driver is not insured. If the marital status, sex and age of the driver are the inputs, Write a PYTHON program to determine whether the driver is insured or not *** ABOVE AVERAGE PROGRAMS
  • 36. 3. ITERATION OR LOOPING ITERATION
  • 37. 3. ITERATION OR LOOPING What is loop or iteration? Loops can execute a block of code number of times until a certain condition is met. OR The iteration statement allows instructions to be executed until a certain condition is to be fulfilled. The iteration statements are also called as loops or Looping statements.
  • 38. 3. ITERATION OR LOOPING Python provides two kinds of loops & they are, for loop while loop
  • 40. while loop A while loop allows general repetition based upon the repeated testing of a Boolean condition The syntax for a while loop in Python is as follows: while condition: body Where, loop body contain the single statement or set of statements (compound statement) or an empty statement. Contd.. : Colon Must
  • 41. while loop The loop iterates while the expression evaluates to true, when expression becomes false the loop terminates. while loop FLOW CHART
  • 42. while loop – Programming example
  • 43. while loop - programs OUTPUT # Natural Numbers generation
  • 44. while loop - programs OUTPUT # Calculating Sum of Natural Numbers
  • 45. while loop - programs #Generating Fibonacci numbers
  • 46. while loop - programs #Generating Fibonacci numbers OUTPUT
  • 47. while loop - Programs Class work / Home Work
  • 48. while loop - programs 1. Write a PYTHON program to print the natural numbers up to n 2. Write a PYTHON program to print even numbers up to n 3. Write a PYTHON program to print odd numbers up to n 4. Write a PYTHON program to print sum of natural numbers up to n BELOW AVERAGE PROGRAMS
  • 49. while loop - programs 5. Write a PYTHON program to print sum of odd numbers up to n 6. Write a PYTHON program to print sum of even numbers up to n 7. Write a PYTHON program to print natural numbers up to n in reverse order. 8. Write a PYTHON program to print Fibonacci series up to n 9. Write a PYTHON program find a factorial of given number AVERAGE PROGRAMS
  • 50. while loop - programs 10. Write a PYTHON program to check the entered number is prime or not 11. Write a PYTHON program to find the sum of digits of given number 12. Write a PYTHON program to check the entered number is palindrome or not 13. Write a PYTHON program to reverse the given number. ABOVE AVERAGE & HOTS PROGRAMS
  • 51. while loop - programs 14. Write a PYTHON program to print the multiplication table 15. Write a PYTHON program to print the largest of n numbers 16. Write a PYTHON program to print smallest of n numbers ABOVE AVERAGE & HOTS PROGRAMS
  • 53. for LOOP Python’s for-loop syntax is a more convenient alternative to a while loop when iterating through a series of elements. The for- loop syntax can be used on any type of iterable structure, such as a list, tuple str, set, dict, or file Syntax or general format of for loop is, for element in iterable: body
  • 54. for LOOP Python’s for-loop syntax is a more convenient alternative to a while loop when iterating through a series of elements. The for- loop syntax can be used on any type of iterable structure, such as a list, tuple str, set, dict, or file Syntax or general format of for loop is, for element in iterable: body
  • 55. for LOOP OUTPUT Till the list exhaust for loop will continue to execute.
  • 57. for LOOP - range KEYWORD The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. range(start, stop, step) for n in range(3,6): print(n) x = range(3, 6) for n in x: print(n) OR
  • 58. for LOOP - range KEYWORD OUTPUT #Generating series of numbers
  • 59. for LOOP - range KEYWORD OUTPUT #Generating even numbers
  • 60. for LOOP – len() FUNCTION
  • 61. for LOOP - range KEYWORD # print string character by character OUTPUT
  • 63. else statement in loop else can be used in for and while loops the else body will be executed as and when the loop’s conditional expression evaluates to false OUTPUT
  • 64. for loop - Programs Class work / Home Work
  • 65. for loop – Programs - Class work / Home Work 1. Write a PYTHON program to print the natural numbers up to n 2. Write a PYTHON program to print even numbers up to n 3. Write a PYTHON program to print odd numbers up to n 4. Write a PYTHON program that prints 1 2 4 8 16 32 … n2 5. Write a PYTHON program to sum the given sequence 1 + 1/ 1! + 1/ 2! + 1/3! + …. + 1/n! BELLOW AVERAGE PROGRAMS
  • 66. for loop – Programs - Class work / Home Work 6. Write a PYTHON program to compute the cosine series cos(x) = 1 – x2 / 2! + x4 / 4! – x6 / 6! + … xn / n! 7. Write a short PYTHON program to check weather the square root of number is prime or not. 8. Write a PYTHON program to produce following design A B C A B C A B C AVERAGE PROGRAMS
  • 67. for loop – Programs - Class work / Home Work 9. Write a PYTHON program to produce following design A A B A B C A B C D A B C D E If user enters n value as 5 ABOVE AVERAGE PROGRAMS
  • 68. for loop – Programs - Class work / Home Work 10. Write a PYTHON program to produce following design A B C D E A B C D A B C A B A (If user enters n value as 5) ABOVE AVERAGE PROGRAMS
  • 69. for loop – Programs - Class work / Home Work 11. Write a PYTHON program to produce following design 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 If user enters n value as 5 ABOVE AVERAGE PROGRAMS
  • 70. for loop – Programs - Class work / Home Work 12. Write a PYTHON program to produce following design 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 If user enters n value as 5 ABOVE AVERAGE PROGRAMS
  • 71. 4. BRANCHING OR JUMPING STATEMENTS
  • 72. 4. BRANCHING OR JUMPING STATEMENTS Python has an unconditional branching statements and they are, 1. break STATEMENT 2. continue STATEMENT
  • 73. 4. BRANCHING OR JUMPING STATEMENTS 1. break STATEMENT Break can be used to unconditionally jump out of the loop. It terminates the execution of the loop. Break can be used in while loop and for loop. Break is mostly required, when because of some external condition, we need to exit from a loop.
  • 74. 1. break STATEMENT break causes jump Condition ? Statement 1 break Loop True
  • 76. 2. continue STATEMENT The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.
  • 77. 2. continue STATEMENT continue causes jump Condition ? Statement 1 continue Loop True Statement n Statements ignored or skipped False
  • 78. 2. continue STATEMENT when i value becomes 2 the print statement gets skipped, continue statement goes for next iteration, hence in the out put 2 is not printed
  • 80. pass STATEMENT The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example):
  • 81. pass STATEMENT pass in loop pass in loop has no output
  • 82. Difference Between break and continue break continue
  • 83. Difference Between break and continue BREAK CONTINUE It terminates the execution of remaining iteration of the loop. It terminates only the current iteration of the loop. 'break' resumes the control of the program to the end of loop enclosing that 'break'. 'continue' resumes the control of the program to the next iteration of that loop enclosing 'continue'. It causes early termination of loop. It causes early execution of the next iteration. 'break' stops the continuation of loop. 'continue' do not stops the continuation of loop, it only stops the current iteration.
  • 85. CLASS TEST Time: 40 Min Max Marks: 20 1. Explain the types control structures 05 2. Write a program to check number is prime or not 05 3. Write a program to check the entered number is Armstrong number or not 05 4. Differentiate between break and continue 05