[go: up one dir, main page]

0% found this document useful (0 votes)
2 views9 pages

Looping Constructs

Uploaded by

mdeshan072009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Looping Constructs

Uploaded by

mdeshan072009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

ARMY PUBLIC SCHOOL SRINAGAR

Informatics Practices
Class XI

Unit-II (Introduction to Python)


Topic: Looping Constructs

Hilal Ahmed Shah


Iteration Statements (Loops)
Iteration statements(loop) are used to execute a block
of statements as long as the condition is true.
Loops statements are used when we need to run same
code again and again.

Python Iteration (Loops) statements are of three type :-

1. While Loop

2. For Loop

3. Nested For Loops


Iteration Statements (Loops)
1. While Loop
It is used to execute a block of statement as long as a
given condition is true. And when the condition become
false, the control will come out of the loop. The
condition is checked every time at the beginning of the
loop.
Syntax
while (condition):
statement
[statements]
e.g.
x=1 Output
while (x <= 4): 1
print(x) 2
3
x=x+1 4
Iteration Statements (Loops)
While Loop continue
While Loop With Else
e.g.

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

You might also like