0% found this document useful (0 votes)
4 views19 pages

Ch4 Loop Statements

The document explains iterative statements, specifically focusing on loop statements in programming, which allow execution of code multiple times until a condition is met. It details two types of loops: 'for' loops and 'while' loops, providing syntax, examples, and explanations of their functionality. Additionally, it covers control statements like 'break' and 'continue', as well as the concept of nested loops.

Uploaded by

sheik8610
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)
4 views19 pages

Ch4 Loop Statements

The document explains iterative statements, specifically focusing on loop statements in programming, which allow execution of code multiple times until a condition is met. It details two types of loops: 'for' loops and 'while' loops, providing syntax, examples, and explanations of their functionality. Additionally, it covers control statements like 'break' and 'continue', as well as the concept of nested loops.

Uploaded by

sheik8610
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/ 19

ch 4 : LOOP Statement

ITERATIVE STATEMENS

ITERATIVE STATEMENTS

An iterative or loop statement allows us to execute a statement or group of statements


multiple times until the condition is true.

It stops when the condition becomes false.

NEED OF ITERATIVE/LOOPING 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.

Loops are used to execute the identical code repeatedly.

TYPES OF ITERATIVE STATEMENTS

There are two types of iterative/looping statements.


1. FOR LOOP
2. WHILE LOOP
for LOOP STATEMENT

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:

for loop_variable in sequence:

statement-block

for i in [1,2,3,4,5] :

print('HELLO')

WORKING OF FOR LOOP

The loop_variable is assigned the first value present in the sequence.

Next the statement block is executed.

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.

The loop body/suite is repeated by the for loop.

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

for with range() function

The range() function is used when we need to iterate over a sequence number

range([beg], end, [step])


# beg, step is optional

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

# Prints HELLO 5 times on the screen

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

for k in range (1, 11):

print( k)

The loop will execute 10 times and will display numbers from 1 to 10.

for m in range (100, 0, -25):

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)

Prints out the numbers

0
1

ange(10, 13):

print(m)

Prints out

10

11

12

print(n)

Prints out

10

15
20

for a in range(-10, -100, -20):

print(n)

Prints out

-10

-30

-50

-70

-90

while STATEMENT

While loop repeats itself as long as a given condition is true.

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

Q WAP to input a number and display the sum of its digits.

sum=0

i=eval(input('Enter a number'))

while i>0:

a=i%10

i=i//10

sum =sum + a

print("sum of digits =",sum)

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.

ELSE CLAUSE ON LOOPS


Loop statements may have an else clause.

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

The break statement enables a program to skip over a part of code.


A break statement terminates out of the innermost enclosing for or while loop.

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.

The "else" part of the loop is executed even if there is a continue

DIFFERENCE BETWEEN BREAK, CONTINUE

BREAK

Break statement causes the loop to break/ terminate immediately.


The loop of which, break statement is a part, stops.
CONTINUE

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;

for i in range (10, 20, 3):

s+=i

if(i==16):

break

print(s);

print("end");

OUTPUT:

20

33

end
s=10;

for i in range (10, 20, 3):

s+=i

if(i==16):

continue

print(s);

print("end");

OUTPUT:

20

33

68

end

NESTED LOOPS

Nested loop is a loop declared inside another loop.

NOTE: We can also nest while loop inside the for loop and vice versa.

WORKING OF NESTED LOOP


EXAMPLE

Give the output of the following code fragment:

for i in range (1, 5, 1):

for j in range (1,i):

print('*', end=' ')

print('\n')

OUTPUT

**

***

You might also like