
Iterative | Looping Statements in Python | CBSE Class 11 | Computer Science
Dear Class 11th STUDENTS,
Welcome to this tutorial of Iterative | Looping Statements in Python from your CBSE class 11 of Computer Science Syllabus .
In this tutorial, we shall be learning our chapter-7 from Unit 2: Computational Thinking & programming-1 as CBSE BOARD suggested to learn about computer system and its organisation to complete this section.
After going through this tutorial, you will be able to understand the following topics from your latest syllabus 2025-26.
Unit 2: Computational Thinking & programming-1
Chapter 7: Iterative | Looping Statements in Python
- for loop,
- range (),
- while loop,
- flowcharts,
- break and continue statements,
- nested loops,
- suggested programs: generating pattern, summation of series, finding the factorial of a positive number, etc.
I advice you to check the latest syllabus given by CBSE Board at its Official website: www.cbseacademic.nic.in
Also, in this tutorial we will covers all necessary topics/concepts required to complete your exams preparations in CBSE classes 11th.
Also, you can Sign Up our free Computer Science Courses for classes 11th and 12th.
NOTE:
- We are also giving some important Questions & Answers for better understanding as well as preparation for your examinations.
- You may also download PDF file of this tutorial from our SHOP for free.
- For your ease and more understanding, we are also giving the video explanation class of each and every topic individually, so that you may clear your topics and get success in your examinations.
Introduction to Iterative | Looping Statements in Python
Iterative statements, also known as looping or repetition statements, are fundamental control flow structures in Python used to execute a block of code repeatedly as long as a specified condition remains true or for each item in a sequence. For CBSE Class 11, the primary iterative statements to understand are the for
loop and the while
loop
THE range () FUNCTION
Before we start with loops, especially for loop of python, let us discuss the range () function of python, which is used with python for loop. the range () function of python generates a list which is a special sequence type. A sequence in python is a succession of value bound together by a single name. some python sequence types are: strings, lists, tuples etc. (see figure below). There are some advance sequence types also but those are beyond the scope of our discussion.
Let us see how range () function works. The common use of range () is in the form given below:
Range (<lower limit >, <upper limit >) # both limits should be integers
The function in the form range (1, u) will produce a list having starting value starting l, l +1, l+2 … (u-1) (l and u being integers). Please note that the lower limit is included in the list but upper limit is not included in the list, e.g.,
Will produce list as [0,1,2,3,4 ].
As these are the numbers in arithmetic progression (a.p.) that begins lower limit 0 and goes up till upper limit
minus 1 i.e., 5 – 1 = 4.
Will return empty [] as no number falls in the a.p. beginning with 5 and ending at 0 (difference d or step-value = +1).
Will give a list as [12,13,14,15,16,17]
All the above example of range () produces numbers increasing by value value 1. What if you want to produce a list with numbers having gap other than 1, e.g., you want to produce a list like [2,4,6,8] using range () function?
For such lists, you can follow from of range () function:
Range (<lower limit>, <upper limit>, <step value >) #all value should be integers
That is, function
Will produce a list having value as l, l + s, l + 2s, … < = u – 1.
Will produce list as [0, 2, 4, 6, 8]
Will produce list as [5, 4,3, 2, 1].
Another from of range () is:
Range (<number>)
That create a list from 0 (zero) to <number> -1, e.g., consider following range () function:
Range (5)
The above function will produce list as [0,1,2,3,4]. Consider some examples of range () function:
The for loop
The for-loop Python is designed to process the items of any sequence, such as a list or a string one by one. The general from of for loop is as given below:
For example, consider the following loop:
A for loop in Python is processed as:
- The loop – variable is assigned the first value in the sequence.
- all the statements in the body of for loop are executed with assigned value of loop variable (step 2)
- once step 2 is over, the loop – variable is assigned the next value in the sequence and the loop-body is executed (i.e., step 2 repeated) with the new value of loop-variable.
- This continues until all values in the sequences are processed.
That is, the given for loop will be processed as follows:
- firstly, the loop-variable a will be assigned first value of list i.e 1 and the statements in the body of the loop will be executed with this value of a. Hence value 1 will be printed with statement print (a) and value 1 will again be printed with print (a*a). (See execution shown on right)
- Next, a will be assigned next value in the list i.e., 4 and loop-body executed. Thus 4 (as result of print (a)) and 16 (as result of print (a*)) are printed.
- Next, a will be assigned next value in the list i. e, 7 and loop-body executed. Thus 7 (as result of print (a*a)) and 49 (as result of print (a*a) ) are printed.
- All the values in the list are executed, hence loop ends.
Therefore, the output produced by above for loop will be:
Consider another for loop:
for Ch in ‘calm’:
print (Ch)
The above loop will produce output as:
(Variable Ch given values as ‘c’, ‘a’, ‘I’, ‘m’, – one at a time from string ‘calm’,)
Here is another for loop that prints the cube of each value in the list :
For v in [1,2,3]:
Print (v*v*v)
The above loop will produce output as:
1
8
81
The for loop works with other sequence types such as tuples also, but we ‘I Stick here mostly to lists to process a sequence of numbers through for loops.
The while Loop
A while loop is a conditional loop that will repeat the instructions within itself as long as a conditional remains true (Boolean True or truth value truetval).
The general form of Python while loop is:
While <logical expression>:
loop- body
where the loop-body may contain a single statement or multiple statements or an empty statement (i.e, pass statement). The loop iterates while the logical expression evaluates to true. When the expression becomes false, the program control passes to the line after the loop-body.
To understand the working of while loop, consider the following code :
Let us see how a while loop is processed:
Step: 1 The logical/conditional expression in the while loop (e.g., a > o above) is evaluated.
Step: 2 Case I if the result of step 1 is true (True or truetval) then all statements in the loop’s body are executed, (see section (i) & (ii) on the right).
Case II if the result of step 1 is false (False or falsetval) then control moves to the next statement after the body- of the loop i.e, loop ends. (See section (iii) on the right).
Step 3 Body-of-the loop gets executed. The step comes only if Step 2, Case I executing, i.e, the result of logical expression two statements of loop body.
After executing the statements of lop-body again, Step 2 and Step 3 are repeated as long as the condition remains true.
The loop will end only when the logical expression evaluates to false. (i.e, Step 2, Case II is executed.)
Nested Loop
A loop may contain another loop in its body. This form of a loop is called nested loop. But in a nested loop, the inner loop must terminate before the outer loop. The following is an example of a nested loop, where a for loop is nested within another for loop.
for i in range (1, 6):
for j in range (1, i): end= ‘ at the end to cause printing at same line
print (‘’*”, end = ‘’)
print () # to cause printing from next line.
The output produced by above code is:
*
* *
* * *
* * * *
The inner for loop is executed for each value of i. The variable i takes values 1, 2, 3 and 4. The inner loop is executed once for i = 1 according to sequence in inner loop range (1, i) (because for i as 1, there is just one element 1 in range (1,1) thus j iterates for one value, i.e , 1) twice for i = 2 (two elements in sequence range (1,i) thrice for i =3 and four times for i= 4.
While working with nested loops, you need to understand one thing and that is, the value of outer loop variable will change only after the inner loop is completely finished (or interrupted).
To understand this, consider the following code fragment:
for outer in range (5, 10, 4):
for inner in range (1, outer ,2):
print (outer, inner)
The above code will produce the output as:
See the explanation below:
Let us understand how the control moves in a nested loop with the help of one more example :
for a in range (3):
for b in range (5, 7):
print (‘’for a = ‘, a, ‘’b now is ‘’, b)
The above nested loop will produce following output:
For the outer loop, firstly a takes the value as 0 and for a = 0, inner loop iterates as it is part of outer form’s loop-body.
- For a =0, the inner loop will iterate twice for values b = 5 and b= 6
- hence the first two lines of output given above are produced for the first iteration of outer loop (a =0; which involves two iterations of inner loop)
- when a takes the next value in sequence, the inner will again iterate two times with values 5 and 6 for b.
Thus, we see, that for each iteration of outer loop, inner loop iterates twice for values b = 5 and b=6.
Consider some more examples and try understanding their functioning based on above lines.
For a in range (3):
For b in range (5, 7):
print ()
The output produced would be:
* *
* *
* *
Consider another nested loop:
for a line range (3):
for b in range (5,8):
print (‘’*’’, end = ‘ ‘ ) notice end = ‘at the end of print
print ()
The output produced would be:
* * *
* * *
* * *
The break Statement in a Nested loop
If the break statement appears in a nested loop, then it will terminate the very loop it is in. That is, if the break statement is inside the inner loop then it will terminate the inner loop only and the outer loop will continue as it as. If the break statement is in outer loop, then outer loop gets terminated. Since inner loop, is part of outer loop’s body it will also not execute just like other statement of outer loop’s body it will also not execute just like other statement of outer loop’s body.
Following program illustrates this. Notice that break will terminate the inner loop only while the outer loop will progress as per its routine.
jump statement – break and continue
python offers two jump statement to be used with in loops to jump out of loop-iteration. these are break and continue statement. let us see how these statement work.
The break statement
The break statement enables a program to skip over a part of the code. A break statement terminates the very loop it lies within. execution resumes at the statement immediately following the body of the terminated statement.
The following figure (fig.8.7) explains the working of a break statement:
The following code fragment gives you an example of a break statement:
a = b = c = 0
for I in range (1, 21):
a = int (input (”enter number 1:”))
b = int (input (”enter number 2:”))
if b == 0:
print (”division by zero error! aborting!”)
break
else:
c = a//b
print (“quotient = “, C)
print (”program over!”)
The above code fragment intends to divide ten pairs of numbers by inputting two numbers a and b in each iteration. if the number b is zero, the loop is immediately terminated displaying message ‘Division by zero error! aborting! otherwise the numbers are repeatedly input and their quotients are displayed. sample run of above code is given below:
Enter number 1: 6
Enter number 2: 2
Quotient = 3
Enter number 1: 8
Enter number 2: 3
Quotient = 2
Enter number 1: 5
Enter number 2: 0
Division by zero error! aborting!
program over!