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

Iteration Statements in Python

The document discusses iteration statements in Python, including 'for' loops, 'while' loops, and nested loops, detailing their syntax and usage. It explains the 'range()' function, as well as the 'break', 'continue', and 'pass' statements, providing example codes for each. Additionally, it compares the functionalities of 'break' and 'continue', and emphasizes the need for iteration in programming.

Uploaded by

Anamika Singh
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 views12 pages

Iteration Statements in Python

The document discusses iteration statements in Python, including 'for' loops, 'while' loops, and nested loops, detailing their syntax and usage. It explains the 'range()' function, as well as the 'break', 'continue', and 'pass' statements, providing example codes for each. Additionally, it compares the functionalities of 'break' and 'continue', and emphasizes the need for iteration in programming.

Uploaded by

Anamika Singh
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/ 12

ITERATION STATEMENTS IN PYTHON

●​ Need for Iteration Statements

●​ for loop Statements

●​ while Statements

●​ The range() Function

●​ break and continue Statements

●​ pass Statement

●​ Nested Loop

QUESTIONS

1.​ Describe the various iteration statements in Python.

2.​ Describe the syntax of while loop with simple example codes in Python.

3.​ Describe the syntax of for loop with simple example codes in Python.

4.​ Compare the use of break and continue statements.

5.​ Draw the flowchart of while and for loop explaining their control flow.

6.​ Compare the use of break and continue statements in Python.

7.​ Explain range() function. How do you use a for loop with range () function in Python?

Need for Iteration Statements

Iteration or loop comes in situations where the same task has to be repeated or some set of instructions have to
be executed repeatedly, either a specific number of times or until a particular condition is satisfied or not
satisfied.

Python programming language provides following types of loops to handle looping requirements:

1.​ while

2.​ for

3.​ Nested
While Statements

●​ A while loop is used to repeatedly execute a block of code as long as a certain condition is true.

●​ Use while loop when you don't know how many times you need to execute the loop beforehand.

Syntax : while expression:


statement

Code : Prints n natural numbers

n=int(input("Enter n : "))
i=1
print("Printing Numbers upto ",n)
while i<=n:
print(i)
i=i+1

Output:

Enter n : 11
Printing Numbers upto 11
1
2
3
4
5
6
7
8
9
10
11
Code : Printing the even numbers till n.

n=int(input("Enter n : "))
i=1
print("Printing Even Numbers upto",n)
while i<=n:
if i%2==0:
print(i)
i=i+1

Output:

Enter n : 10
Printing Even Numbers upto 10
2
4
6
8
10

Code : To find the sum of first n natural numbers using a while loop.

n=int(input("Enter n : "))
sum=0
i=1
while i<=n:
sum=sum+i
i=i+1
print("The sum is", sum)

Output:

Enter n : 10
The sum is 55

Code: Printing the elements of a list

mylist=[2,4,6,8,10,12,14,16]
print(len(mylist))
print(mylist)
index=0
while index<len(mylist):
print("Index is : " , index , "Elements are: " ,mylist[index])
index=index+1
Output:

8
[2, 4, 6, 8, 10, 12, 14, 16]
Index is : 0 Elements are: 2
Index is : 1 Elements are: 4
Index is : 2 Elements are: 6
Index is : 3 Elements are: 8
Index is : 4 Elements are: 10
Index is : 5 Elements are: 12
Index is : 6 Elements are: 14
Index is : 7 Elements are: 16

Code : while with if ( Searching an element in list )

mylist=[2,2,2,5,2,3,6,8,9,10]
print(len(mylist))
print("List : ", mylist)
a=int(input("enter a number :"))
i=0
m=0
while i<len(mylist):
if a==mylist[i]:
print("number at" ,i+1,"th position")
print("number at" ,i,"th Index")
m=m+1
i+=1
if m==0:
print("number is not found")

Output :

10
List : [2, 2, 2, 5, 2, 3, 6, 8, 9, 10]
enter a number :5
number at 4 th position
number at 3 th Index
BREAK AND CONTINUE STATEMENTS

The break statement is used to exit from a loop immediately without completing the rest of the iterations.

SYNTAX:

In while loop,

while (Test expression):


if condition_for_break:
break;
else:
//codes to repeat in loop

In for loop,

for (Initialisation, Condition, Update):


if condition_for_break:
break;
else:
//codes to repeat in loop
Code : Example of break statement

n=int(input("Enter n : "))
i=1
print("Printing Numbers upto",n)
while i<=n:
print(i)
if(i==5):
break
i=i+1

Output:

Enter n : 10
Printing Numbers upto 10
1
2
3
4
5

The continue statement jumps back to the top of the loop.


SYNTAX:

In while loop,

while (Test expression):


if condition for skipping:
continue;
else:
//codes to repeat in loop

In for loop,

for (Initialisation, Condition, Update):


if condition for skipping:
continue;
else:
//codes to repeat in loop

Code : Example of continue statement

n=int(input("Enter n : "))
i=1
print("Printing Numbers upto",n)
while i<=n:
if(i==5):
i=i+1
continue
print(i)
i=i+1

Output:

Enter n : 10
Printing Numbers upto 10
1
2
3
4
6
7
8
9
10
Code: Example of break statements (Multiplication table of 5 )

i=0
while True:
i=i+1
print("5*",i,"=",(5*i))
if i>=10:
break

Output:

5* 1 = 5
5* 2 = 10
5* 3 = 15
5* 4 = 20
5* 5 = 25
5* 6 = 30
5* 7 = 35
5* 8 = 40
5* 9 = 45
5* 10 = 50

Code : To print the even numbers upto n, which is highest number entered by the user

n=int(input("Enter value of n : "))


i=1
print("Printing even numbers upto",n)
while i<=n:
i=i+1
if i%2!=0:
continue
print(i)

Output:

Enter value of n : 10
Printing even numbers upto 10
2
4
6
8
10
NOTE:Both break and continue are used with while loop statements used to stop the normal execution in
loops or iterations. The break statement exits from the complete loop if a condition is met. Whereas, the
continue statement causes execution to terminate the current iteration and immediately continue at the start of
the loop and start the next iteration. Thus, continue only skips the current iteration while break skips the
complete loop.

NOTE:The break statement terminates the execution of the loop whereas the continue statement skips
the particular iteration and proceeds to the next iteration and does not terminate the loop completely.

FOR LOOP STATEMENTS

Another option in Python for implementing iteration or loop is the for statement. The syntax is shown below:

SYNTAX: for variable_name in list_name:


body of loop

Code: 1

mylist=[2,4,6,8,10,12,14,16]
print(len(mylist))
print(mylist)
for elements in mylist:
print(elements)
Output:

8
[2, 4, 6, 8, 10, 12, 14, 16]
2
4
6
8
10
12
14
16

Code:2

str="WELCOME"
i=1
for letter in str:
print(i,"Letter : ",letter)
i=i+1

Output:

1 Letter : W
2 Letter : E
3 Letter : L
4 Letter : C
5 Letter : O
6 Letter : M
7 Letter : E

THE RANGE() FUNCTION

The range() function is a built-in function in Python that is used in for loop function for generating a sequence
of numbers which can be assigned to the loop variable.

The statement range(n) starts from 0 and ends till n - 1.

In other words, Range functions return a sequence of numbers, starting from 0 by default, and increments by 1
(by default), and stops before a specified number.

SYNTAX: range (start, end, step)


Code: Check code where range(5) assigns values from 0 to 4 for the loop variable i .

range(end)

for i in range(5):
print(i)

Output:

0
1
2
3
4

The function range (a, b) will print from a to b-1 (excluding b) in order with default step size of 1.

range(start,end)

Code:

for i in range(5,10):
print(i)

Output:

5
6
7
8
9

To specify step size, we can add step size or incrementing value as the third argument to range(). Check
Code where it prints 1 to 9 with a gap of 3. Hence, the values are 1, 4, 7.

range (start, end, step)

Code:

for i in range(1,10,3):
print(i)
Output:

1
4
7

NOTE: range() is a built-in function in Python to generate a range of numbers. It takes a set of values
consecutively that successively increase by a specified step size, or 1 by default, if not specified in the
function.

You might also like