Python 5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Repetition Control

Section A

Dr. Emmanuel S. Pilli


Associate Professor, CSE
Control Structures

 Statements in a program are executed one after the other in


the order in which they are written - Sequential execution
 Python statements can enable to specify the next statement to
be executed which is other than the next one in sequence -
Transfer of control
 Programs can be written in terms of three control structures
 Sequence structure
 Selection / Decision Control structure
 Iteration / Repetition Control structure
Iterative Statements
 Python provides two types of iteration structures in the
form of statements
 while
 for
while statement

 An iteration statement allows you to specify that an


action is to be repeated while some condition remains
true.
 The action will be performed repeatedly while the
condition remains true.
 The statement(s) contained in the while iteration
statement constitute the body of the while.
while statement
 The while statement body may be a single statement or a
compound statement.
 Once the condition will become false, the iteration
terminates, and the first statement after the iteration
structure is executed

 A loop is a group of instructions the computer executes


repeatedly while some loop-continuation condition remains
true.
while statement
 initialization
 while continuationCondition :
do main action to be repeated
prepare variables for the next time through the loop

 while is used to repeatedly execute instructions as


long as condition is true
while statement
while statement
 initialization
 while condition :
statement1
statement2
else:
statement3
statement4

 else block is optional and is executed when condition fails


while statement
Counter Controlled Iteration
 counter-controlled iteration uses a variable called a
counter to specify the number of times a set of
statements should execute
 Also called definite iteration because the number of
iterations is known before the loop begins executing
Counter Controlled Iteration

 In counter-controlled iteration, a control variable is used to


count the number of iterations.
 The control variable is incremented (usually by 1) each time
the group of instructions is performed.
 When the value of the control variable indicates that the
correct number of iterations has been performed, the loop
terminates
 Execution continues with the statement after the iteration
statement.
Counter Controlled Iteration
 Variable is initialized to be used as a counter
 Variable is used in an expression which is checked for a
condition
 Variable is modified (incremented or decremented or
arithmetic operators are applied) in each iteration
while statement
 A while loop can be used in the following conditions
 Repeat a set of statements till a condition remains True
(the number of times the statements are executed is not
known beforehand)
 Repeat a set of statements a finite number of times
 Iterate through a string, list and tuple using an index value
break
 break statement terminates the loop without
executing the else block

 break statement exits from the loop and transfers


the execution from the loop to the statement that
immediately following the loop
continue
 continue statement skips the rest of the
statements in the block and continues with the
next iteration of the loop.

 continue statement causes execution to


immediately continue at the start of the loop, it
skips the execution of the remaining body of the
loop.
for
 for is used to iterate over elements of a sequence
such as string, tuple, list, dictionary or other
iterables
 In iteration step, the loop variable is set to a value
 The for loop is used to iterate over a sequence
 for item in sequence:
indented statements to repeat; may use item
for
 for var in list :
statement1
statement2
 for var in list :
statement1
statement2
else:
statement3
statement4
range function
 The built in function range( ) helps to iterate over a
sequence of numbers, It produces an iterator that follows an
arithmetic progression
 range(n)generates a sequence of numbers from 0 to n – 1
 range( )function can also be passed with two arguments
begin and end. It generates a sequence of numbers that
starts from begin and ends with end.
 We can use another parameter step to increase the
difference between two numbers
for
 A for loop can be used in following two situations
 Repeat a set of statements a finite number of times
 Iterate through a string, list, tuple, set or dictionary
 for char in 'Leopard':
print(char, end = ' ‘)

 for animal in ['Cat', 'Dog', 'Tiger', 'Lion','Leopard’]:


print(animal, end = ' ‘)

 for flower in ('Rose', 'Lily', 'Jasmine’):


print(flower, end = ' ‘)
for
 A for loop can be used in following two situations
 Repeat a set of statements a finite number of times
 Iterate through a string, list, tuple, set or dictionary

 for num in {10, 20, 30, 40, 50}:


print(num, end = ‘ ‘)

 for key in {'A101':'Rajesh', 'A111': 'Sunil’,


'A112': 'Rakesh’}:
print(key, end = ' ')
Set
for num in {10, 20, 30, 40, 50}:
print(num, end = ' ')
print()
for num in {50, 30, 40, 20, 10}:
print(num, end = ' ')
print()
for num in {30, 10, 20, 50, 40}:
print(num, end = ' ')
print()
for num in {20, 40, 50, 10, 30}:
print(num, end = ' ')
print()
for num in {40, 50, 10, 30, 20}:
print(num, end = ' ')
Set
for num in {10, 10, 10, 10, 10}:
print(num, end = ' ‘)

for num in {10, 20, 10, 20, 10}:


print(num, end = ' ')
Else blocks in Loop
 else block of a while loop must be used in situations
 where some statements need to be executed if the loop is
terminated normally
 and
 where some statements need not be executed if the loop is
terminated abruptly
Use of else block and break in while
n = int(input("Enter a number: "))
i=2
while i <= n/2 :
if (n % i) == 0:
print(n, "is not a prime number")
break
i += 1
else:
print(n, "is a prime number")
Use of else block and break in for
for el in [10, 20, 30, 3, 40, 50]:
if el % 10 != 0:
print(el, 'is not a multiple of 10')
break
else:
print('All numbers are multiples of 10')

You might also like