Definition : Repeated execution of a set of statements is called iteration.
Iteration
AU
: Dec.-19, May 19, Marks 16
•
Iteration is a technique that allows to execute a block of statements
repeatedly.
Definition
: Repeated
execution of a set of statements is called iteration.
•
The programming constructs used for iteration are while, for, break, continue
and so on.
•
Let us discuss the iteration techniques with the help of illustrative examples.
•
The simple form of statement is assignment Statement. The statement is
specified using = operator.
•
The reassignment statement is specified as
•
Reassigning variables is often useful, but you should use it with caution. If
the values of variables change frequently, it can make the code difficult to
read and debug.
•
Similarly we can update the values by using operators. For example :
The
while statement is popularly used for representing iteration.
Syntax
while
test_condition:
body
of while
Flowchart
for while statement is as given below
The
flow of execution is specified as follows –
1.
Using the condition determine if the given expression is true or false.
2.
If the expression is false then exit the while statement
3.
If the expression is true then execute the body of the while and goback to step
1 in which again the condition is checked.
For
example
while
i< = 10:
i
= i + 1
•
The body of a while contains the statement which will change the value of the
variable used in the test condition. Hence finally after performing definite
number of iterations, the test condition gets false and the control exits the
while loop.
•
If the condition never gets false, then the while body executes for infinite
times. Then in this case, such while loop is called infinite loop.
•
There is another version of while statement in which else is used.
Syntax
while
test_condition:
body
of while
else:
statement
Example
while
i < =10:
i
= I + 1
else:
print("Invalid
value of i")
Programming
Examples on while
Example
3.5.1 Write a python program for computing the sum of n
natural numbers and finding out the average of it.
Solution
:
The python script is as given
This
program can be run by pressing F5 key and following output can be obtained.
Output
Example
3.5.2 Write a python program to display square of a
numbers using while loop
Solution
:
Example
3.5.3 Write a python program for displaying even or
odd numbers between 1 to n.
Solution
:
Example
3.5.4 Write a python program to display Fibonacci
numbers for the sequence of length.
Solution
:
print("Enter
the value of n")
n
= int(input())
a
= 0
b
= 1
i
= 0
print("Fibonacci
Sequence is...")
while
i < n:
print(a)
c
= a + b
a
= b
b
= 0
i
= i + 1
Output
Enter
the value of n
10
Fibonacci
Sequence is...
0
1
1
2
3
5
8
12
21
34
3.5.3
for
The
for loop is another popular way of using iteration. The syntax of for loop is
Syntax
for
variable in sequence:
Body
of for loop
The
variable takes the value of the item inside the sequence on each iteration.
Loop
continues until we reach the last item in the sequence. The body of for loop is
separated from the rest of the code using indentation.
Example
for
val in numbers:
val
= val + 1
Similarly,
we can have for loop with else statement
Syntax
for
variable in sequence:
Body
of for loop
else:
Statement
Example
for
i in myList:
print(i)
else:
print("The
number is not present in your list")
Programming
examples based on For Loop
Example
3.5.5 Write a python program to find the sum of 1 to
10 numbers.
Solution
:
Example
3.5.6 Write a python program to display the
multiplication table.
Solution
:
print("Enter
number for its multiplication table")
n
= int(input())
for
i in range(1,11):
print(n,"X",i,i*n)
Output
Example
3.5.7 Write a program to print 1+1/2+1/3+1/4+ ……. +1/N series.
Solution
:
print("Enter
value of N")
n
= int(input())
sum= 0
for
i in range(1,n+1):
sum
= sum+1/i
print("The
Sum is ",sum)
Output
Example
3.5.8 Write a Python program to check whether given
number is prime or not.
Solution
:
#
User can enter the value thru keyboard
print("Enter
the number");
num
= int(input())
#
prime numbers are greater than 1
if
num > 1:
#
check for factors
for
i in range(2, num):
if
(num%i) = = 0:
print(num,"is
not a prime number")
break
else:
print(num,"is
a prime number")
#
if input number is less than
#
or equal to 1, it is not prime
else:
print(num,"is
not a prime number")
Output
Example
3.5.9 Write a Python Program to find the prime numbers
between given interval.
Solution
:
lower
= int(input("Enter lower range: "))
upper
= int(input("Enter upper range: "))
print("Prime
numbers between",lower,"and", upper,"are:")
for
num in range(lower,upper + 1):
#
prime numbers are greater than 1
if
num > 1:
for
i in range(2,num):
if
(num %i) = = 0:
break
else:
print(num)
Output
Example
3.5.10 Write a python program to display the star pattern
as
*
**
***
****
*****
...
...
user should enter the value of N
Solution
:
print("Enter
value of n")
n
= int(input())
for
i in range(0,n):
for
j in range(0,i+1):
print('*
',end="")
print('')
Output
Example
3.5.11 Write a python program to display the number
pattern as follows
1
2 3 4 5
2
3 4 5
3
4 5
4
5
5
Solution
:
for
i in range (1,6):
for
k in range(1,i):
print(end='')
for
j in range(1,6):
print("
",jend="")
print();
Output
Example
3.5.12 Write a python program to print the pattern as
given below
A
A
B
A
B C
A
B C D
A
B C D E
Solution
:
for
i in range(1, 6):
for
j in range(65, 65+i):
a
= chr(j)
print(a,"
",end ="')
print()
Output
Example
3.5.13 Write a python program to print the pattern for
alphabets
A
A A A A
В
В В В
C
C C
D
D
E
Solution
:
num
= 65 #ASCII Value for A
for
i in range(0,5):
for
j in range(1,5):
ch
= chr(num+i) # Converting ASCII to character
print(ch,"
",end='')
print();
Output
•
The break statement is used to transfer the control to the end of the loop.
•
When break statement is applied then loop gets terminates and the control goes
to the next line pointing after loop body.
Syntax
break
For
example
for
i in range(1,11):
if
i = =5:
print("Element
{} Found!!!".format(i))
break
print(i)
Output
•
The continue statement is used to skip some statements inside the loop. The continue
statement is used with decision making statement such as if...else.
•
The continue statement forces to execute the next iteration of the loop to
execute.
Syntax
continue
Example
In
while loop the continue passes the program control to conditional test.
Following example illustrates the idea of continue
i
= 0
while
i < 10:
i
= i + 1
if
i%2 = = 0:
continue
print(i)
Output
6. pass
The
pass statement is used when we do not want to execute any statement.
Thus the desired statements can be bypassed.
Syntax
Pass
Example
for
i in range(1,5):
if
i= =3:
pass
print("Reached
at pass statement")
print("The
current number is ",i)
Output
Example
3.5.14 Write a Python program to print sum of cubes of
the values of n variables,
AU
: Dec.-19, Marks 2
Solution
:
sum
= 0
n
= int(input("Enter some number: "))
for
i in range(1, n+1):
sum
= sum+(i*i*i)
print("Sum
of cubes of first",n,"numbers is = ",sum)
Output
Enter
some number: 5
Sum
of cubes of first 5 numbers is = 225
>>>
Example
3.5.15 Find the syntax error in the code given while
True print('Hello world')
AU
: Dec.-19, Marks 2
Solution
: There
must be colon after True and the print statement must be indented. The correct
code is as follows –
while
True :
print('Hello
world')
Review Question
1. Mention the different types of iterative structure allowed in
Python. Explain the use of continue and break statements with an example.
AU
: May 19, Marks 16
Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings : Tag: Engineering Python : Flow Chart, Syntax, Example Program | Engineering Python Programming - Iteration
Problem Solving and Python Programming
GE3151 1st Semester | 2021 Regulation | 1st Semester Common to all Dept 2021 Regulation