Ch4 Loop Statements
Ch4 Loop Statements
ITERATIVE STATEMENS
ITERATIVE STATEMENTS
There may be a situation, when you need to execute a block of code several number of
times.
For example, if you are asked to display your name 1000 times on the screen using a
program.
It is not worth writing the same display statement 1000 times in the program.
It may lead to errors and makes the program code unnecessarily lengthy.
To avoid all these problems loops are essential.
The for loop is used to execute a set of statements a fixed number of times.
Usually the number of times the statements are going to execute is known at the start
of the loop.
Python’s for statement iterates over the items of a sequence(list, string, tuple, set,
dictionary etc..), in the order that they appear in the sequence.
SYNTAX:
statement-block
for i in [1,2,3,4,5] :
print('HELLO')
After this the next value present in the sequence is assigned to the loop_variable and
the statement block is executed.
This process continues till the entire sequence is exhausted.
NOTE: The indented statements that follow the for loop forms the suite/loop body.
EXAMPLE
Q Write a loop statement using for loop to display “HELLO” five times on the screen.
for i in [1,2,3,4,5] :
print('HELLO')
OUTPUT
HELLO
HELLO
HELLO
HELLO
HELLO
The range() function is used when we need to iterate over a sequence number
The function range( ) generates a list of values starting from beg till end-1
. if step is given added to the value generated, to get the next value in the list.
The default value for step is 1 i.e. if step parameter is not specified 1 is added at every
iteration.
The beg parameter is also optional. If it is not specified list starts from zero.
EXAMPLE
for i in range(1,6):
print('HELLO')
OUTPUT
HELLO
HELLO
HELLO
HELLO
HELLO
Note: The second number (end here) is not included in the range i.e. all numbers up to
this number are included in the range.
for LOOP: SOME MORE EXAMPLES
print( k)
The loop will execute 10 times and will display numbers from 1 to 10.
print( m)
The loop will execute 4 times and will display numbers: 100, 75, 50, 25. Loop variable
will be decremented by 25 at every step.
inange(4):
print(k)
0
1
ange(10, 13):
print(m)
Prints out
10
11
12
print(n)
Prints out
10
15
20
print(n)
Prints out
-10
-30
-50
-70
-90
while STATEMENT
SYNTAX:
while(test expression):
statement-block
If the test expression is true, statement-block will be executed.
If the test expression evaluates to false, loop will not be executed and control passes to
the statement following the loop.
EXAMPLE
Q Write a loop statement using while loop to display “HELLO” five times on the screen.
i=1;
while i<=5:
print ('HELLO')
i=i+1
OUTPUT
HELLO
HELLO
HELLO
HELLO
HELLO
EXAMPLE OF WHILE LOOP
sum=0
i=eval(input('Enter a number'))
while i>0:
a=i%10
i=i//10
sum =sum + a
OUTPUT
Enter a number234
sum of digits =9
m=100
while m>0:
print (m)
m=m-25
The loop will execute 4 times and will display numbers: 100, 75, 50,25. Loop variable will
be decremented by 25 at every step.
m=100
while m>0:
print(m)
m=m +25
The loop will execute infinitely as value of m is incremented by 25 at every step. It will
always be greater than zero and test expression will never become false
.
k=1
while k>10:
print(k)
k=k+1
The loop will not execute at all because the test expression is false in the beginning
itself. There will be no output.
while 0:
print("hello")
The loop will not execute at all because the test expression has value 0 which is
considered as false in the beginning itself. There will be no output.
while 1:
print("hello ")
The loop will execute infinitely because the test expression has value 1 which is
considered as true. The loop will execute indefinitely.
If the else clause is used with for loop, it is executed when the loop terminates through
exhaustion of the list.
If the else clause is used with while loop, it is executed when the condition becomes
false.
EXAMPLE
for i in [0,1,2]:
print("Hello")
else:
print("Bye")
Hello
Hello
Hello
Bye
i=1
while i<4:
print("Hello")
i=i+1
else:
print("Bye")
Hello
Hello
Hello
Bye
BREAK STATEMENT
Execution resumes at the statement immediately following the body of the terminated
loop.
EXAMPLE1
for i in [0,1,2,3,4]:
if(i>=2):
break
print(i)
print("end")
OUTPUT
end
EXAMPLE 2
i=1
while i<10:
if(i%5==0):
break
print(i)
i=i+1
OUTPUT
CONTINUE STATEMENT
Continue statement causes the current iteration of the loop to skip and go to the next
iteration.
All the statements following the continue statement in the loop will not be executed and
loop control goes to the next iteration.
EXAMPLE
for i in [0,1,2,3,4]:
if(i ==2):
continue
print(i)
print("end")
OUTPUT
end
EXAMPLE
i=1
while i<=10:
i=i+1
if(i%5==0):
continue
print(i)
OUTPUT
11
NOTE:
When the loop is terminated by a break statement, else clause is not executed.
BREAK
Continue statement causes the current iteration of the loop to skip and go to the next
iteration.
All the statements following the continue statement in the loop will not be executed and
loop control goes to the next iteration.
s=10;
s+=i
if(i==16):
break
print(s);
print("end");
OUTPUT:
20
33
end
s=10;
s+=i
if(i==16):
continue
print(s);
print("end");
OUTPUT:
20
33
68
end
NESTED LOOPS
NOTE: We can also nest while loop inside the for loop and vice versa.
print('\n')
OUTPUT
**
***