0% found this document useful (0 votes)
197 views

07 Lecture Iterative Statements

The document discusses iterative statements in Python. It describes the while and for loops. The while loop continuously executes a block of code as long as a condition is true. The for loop iterates over a sequence, like a list or string. It provides examples of using while and for loops, including using else blocks that execute after the loop completes normally without breaking.

Uploaded by

wh14257
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
197 views

07 Lecture Iterative Statements

The document discusses iterative statements in Python. It describes the while and for loops. The while loop continuously executes a block of code as long as a condition is true. The for loop iterates over a sequence, like a list or string. It provides examples of using while and for loops, including using else blocks that execute after the loop completes normally without breaking.

Uploaded by

wh14257
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PYTHON Programming (CST310)

Lecture 07:
Iterative Statements in Python

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
August 17, 2023
Flow Control Statements in Python
The flow control statements are divided into three categories
1. Conditional statements
2. Iterative statements.
3. Transfer statements.
Iterative Statements in PYTHON
• In Python, the iterative statements are also known as looping
statements or repetitive statements.
• The iterative statements are used to execute a part of the program
repeatedly as long as a given condition is True.
Python provides the following iterative statements.
1. while statement
2. for statement
while statement in Python
• In Python, the while statement is used to execute a set of statements repeatedly.
• In Python, the while statement is also known as entry control loop statement
because in the case of the while statement, first, the given condition is verified then
the execution of statements is determined based on the condition result.
• The general syntax of while statement in Python is as follows.
while condition:
Statement_1
Statement_2
Statement_3
...
Python don’t use curly braces for specifying the block of while statements.
Indentation is used in Python for the above purpose.
The indentation is a series of white-spaces. Here, the number of white-spaces may
variable, but all statements must use the identical number of white-spaces.
Example Program of While Statement
n=int(input("Enter no of times you want to print hello"))
while(n>0):
print("Hello")
print(n)
n=n-1
while statement with 'else' clause in Python
• In Python, the else clause can be used with a while
statement.
• The else block gets executed whenever the condition of
the while statement is evaluated to false.
• But, if the while loop is terminated with break statement
then else doesn't execute.
Example Program of while statement with
'else' clause in Python
n=int(input("Enter no of times you want to print hello"))
while(n>0):
print("Hello")
print(n)
n=n-1
else:
print("hello has been printed 5 number of times")
print("job is done")
If condition within While Block
n=int(input("Enter no of times you want to print hello"))
while(n>0):
if(n==5):
print("n is equal to 5.. Exiting")
break
print("Hello")
print(n)
n=n-1
else:
print("hello has been printed 5 number of times")
print("job is done")
if the while loop is terminated with break statement then else
doesn't execute.
for statement in Python
• In Python, the for statement is used to iterate through a sequence like a
list, a tuple, a set, a dictionary, or a string.
• The for statement is used to repeat the execution of a set of statements for
every element of a sequence.
The general syntax of for statement in Python is as follows.
for <variable> in <sequence>:
Statement_1
Statement_2
Statement_3
...
Using For Loop on a String
name=“nit srinagar”
for i in name:
print(i)
Python code to illustrate for statement with
Range function
# Python code to illustrate for statement with Range function
for value in range(1, 6):
print(value)
print('Job is done!’)

Output:
1
2
3
4
5
Python code to illustrate for statement with
Range function
Range(start_value,end_value, step)

Range(1,5) 1234
Range(5) 01234
Range(1,10,2) 13579
range(5,1,-1) 5432
Printing Without Newline
# Python code to illustrate for statement with Range function
for value in range(1, 6):
print(value, end=“”)
print('Job is done!’)

Output:
12345
for statement with 'else' clause in Python
• In Python, the else clause can be used with a for a statement.
• The else block is gets executed whenever the for statement is does
not terminated with a break statement.
• But, if the for loop is terminated with break statement then else block
doesn't execute.

You might also like