Page 97 - Computer - 8
P. 97
Lab Activity 6
Experiential Learning
To accept a number from the user and display the sum of all numbers from 1 to the entered number.
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.
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.
Lab Activity 7 Experiential Learning
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.
95