Page 99 - Computer - 8
P. 99
Knowledge Discovery Subject Enrichment
A loop that gets executed indefinitely without terminating is called an Infinite loop. Usually, infinite loops
occur due to some error in the coding, such as incorrect updation of the counter variable or incorrect logic
for evaluating the test expression. To stop the execution of an infinite loop, press the Ctrl + C key combination.
This causes the interpreter to come out of the execution mode.
JUMP STATEMENTS IN A LOOP IN PYTHON
Sometimes, we may need to end a loop prematurely, earlier than the logical termination of the loop. We
may also need to execute the loop from the start without executing a few statements. Python provides
two statements – Break and Continue, for this purpose. These are called Jump statements as they let
the execution control jump to a specific position in the program.
The Break Statement
The Break statement is used to terminate a loop and transfer the execution control to the statement
outside the body of the loop.
Lab Activity 10
Experiential Learning
To display all numbers from 1 to 10 using the For loop but to terminate the loop as soon as a multiple
of 7 is encountered.
1. Type the code as shown in the image in the Python Editor.
2. Press the F5 key to execute the code. Sample output of the program is given below.
The Continue Statement
The Continue statement causes the interpreter to skip the remaining statements in the body of the loop
and move to the start of the loop block. It transfers the execution control to the test expression in the
loop statement.
97