Iteration or Looping statements
Loop statement allows us to execute a statement or group of statement multiple times.
It is used to execute more times the same set of operations within the loop. It is also known as iteration.
Python provides three conditional looping statements. They are
- For loop
- While loop
- Nested loop
For loop
The for loop repeats a given block of codes by specified number of times. It is used to print the values one by one.
Syntax:
for iterating variable in sequence:
body of for
Flowchart
Example
Number=[1,2,3,4,5]
for i in Numbers:
print(“The current number is:”, i)
Output
The current number is: 1
The current number is: 2
The current number is: 3
The current number is: 4
The current number is: 5
Example
for letter in “python”:
print(“The current letter is:”, letter)
Output
The current letter is: p
The current letter is: y
The current letter is: t
The current letter is: h
The current letter is: o
The current letter is: n
While loop
It repeats a single statement or group of statements until some condition is met. It tests the condition before executing the loop body
Syntax:
while test expression:
body of while
Flowchart
Example
count = 1
while count<5:
print(“The count is:”, count)
count=count+1
Output
The count is: 1
The count is: 2
The count is: 3
The count is: 4
Nested for loop
Syntax:
for iterating variable in sequence:
for iterating variable in sequence:
body of for
body of for
Nested While loop
Syntax:
while test expression:
while test expression:
body of while
body of while



No comments:
Post a Comment