Page 69 - Code & Click - 8
P. 69
WHILE LOOP
The While loop in Python is used to execute a set of instructions repeatedly until a certain condition
is satisfied. As the number of times the While loop is executed is not known beforehand, it is also
known as an Indefinite loop. The syntax of the While loop is as follows:
while <test expression>:
body of loop
other statements
The flowchart to represent the While loop is given in the image.
The While loop has a test expression that contains an already initialised
counter variable. The test condition is evaluated. If the condition
evaluates to True, the While block of statements is executed. The
counter variable gets updated. The test condition is evaluated again.
This process continues until the test condition evaluates to False,
after which the loop terminates and the execution control passes to
the statement after the While loop block.
Experiential Learning
Lab Activity 5
To display all odd numbers between 1 and 10 using While loop.
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.
Experiential Learning
Lab Activity 6
To accept a number from the user and display its table using the While loop.
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.
67