Page 195 - AI_Ver_3.0_class_11
P. 195
Iterative Statements
Iterative statements are also known as looping statements. A loop is used to execute instructions or a block of code
multiple times, without writing it repeatedly. A sequence of instructions when repeated for a number of times or until
a condition is true is called a loop.
In Python, there are two types of conditional statements, which are as follows:
• • The for loop
• • The while loop
The for Loop
for each element
A for loop is used when you know in advance how many times you in sequence
want to execute a block of code. It iterates over a sequence (such as
a list, tuple, string, or range) and executes the block of code for each Last element True
element in the sequence. reached?
The for statement executes a simple or compound statement for a False
fixed number of times.
The syntax of while loop is as follows: Execute statements
inside for block Exit loop
for element in sequence:
statements
Program 18: To demonstrate the use of the for loop
numbers = [1, 2, 3, 4, 5]
for num in numbers:
multiplied = num * 2
print(multiplied)
Output:
2
4
6
8
10
The while Loop
The while statement executes a set of statements repeatedly,
until the logical expression evaluates to True. When the condition Test Expression False
becomes False, the control comes out of the loop.
The syntax of while loop is as follows:
True
while (test expression):
statements Execute statements
inside while block Exit loop
increment/decrement
Python Programming 193

