Looping Constructs
Looping Constructs
Informatics Practices
Class XI
1. While Loop
2. For Loop
x=1
while (x < 3):
print('inside while loop value of x is ',x)
x=x+1
else:
print('inside else value of x is ', x)
Output
inside while loop value of x is 1
inside while loop value of x is 2
inside else value of x is 5
Iteration Statements (Loops)
While Loop continue
Infinite While Loop
e.g.
x=5
while (x == 5):
print(‘inside loop')
Output
Inside loop
Inside loop
…
…
Iteration Statements (Loops)
2. For Loop
It is used to iterate over items of any sequence, such
as a list or a string.
Syntax
for val in sequence:
statements
e.g.
for i in range(3,5):
print(i)
Output
3
4
Iteration Statements (Loops)
2. For Loop continue
Example programs
for i in range(5,3,-1):
print(i)
Output
5
4
range() Function Parameters
start: Starting number of the sequence.
stop: Generate numbers up to, but not including this number.
step(Optional): Determines the increment between each numbers
in the sequence.
Iteration Statements (Loops)
2. For Loop continue
For Loop With Else
e.g.
for i in range(1, 4):
print(i)
else: # Executed because no break in for
print("No Break")
Output
1
2
3
4
No Break
Iteration Statements (Loops)
2. For Loop continue
Nested For Loop
e.g.
for i in range(1,3):
for j in range(1,11):
k=i*j
print (k, end=' ')
print()
Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20