Page 153 - Information_Practice_Fliipbook_Class11
P. 153
n = 8 64 36 16 4 As long as the test condition, n>0 is True, the
while n > 0: Done statements in the body of the while loop will
print(n*n, end=' ') be executed. After the loop terminates normally
n = n-2 (without a break statement), the statement
in the else block print("\nDone") gets
else:
executed.
print('\nDone')
In the case of a while loop, the test condition is checked at the entry point of the loop. So, the loop may not be
executed even once, if the test condition is False. Therefore, always remember to initialize the control variable
before the loop begins. If the initial value is not given to the control variable, the while loop will not execute.
Secondly, the control variable has to be updated inside the while loop, otherwise, the loop will never terminate and
become an infinite loop.
C T 03 1. Consider the code given below:
for num in range(10,30,3):
print(num-1)
Identify the following components of the given loop:
a. Control variable
b. Initial value of control variable
c. Final value of control variable
d. Step value
e. Body of loop
2. Is the following code elegant? If not, rewrite it to make it more elegant:
num = 10
while(num<30):
print(num-1, end = ' ')
num = num+3
6.5 Jump Statements
We have learnt that loops are used to execute the statements repeatedly in the same sequence as they are given in
the body of the loop. However, sometimes, we may require to either exit the loop or skip certain statements of the
loop before moving to the next iteration. Jump statements help to control the loop in such a manner. The two jump
statements provided by Python are break and continue.
6.5.1 break Statement
The break statement terminates the same loop in which it is defined and moves the control to the next statement
immediately following the loop. Once the break statement is encountered and executed, no further statement in the
loop will be executed.
Syntax:
break
The flowchart in Fig 6.4 explains the working of break statement.
Looping in Python 139

