Page 89 - Digicode_AI_class_8
P. 89
Program: To demonstrate the use of a while loop.
On running the above program, you will get the following output:
Never use assignment operator in place of equality operator
in the test condition as it may cause an infinite loop.
The Infinite Loop
If the condition given in a loop never becomes false, then the loop will never terminate and run
indefinitely. This type of loops is called an infinite loop.
Example:
while(1)
print(“Hello”)
Output: Hello will be printed infinite times.
To come out of the infinite loop, we can either close the program window or press Ctrl + C. This will
break the running of the program. In fact, at any time during the execution of a program in Python,
you can press the key combination Ctrl + C to stop running the program.
The while Loop using else Statement
Python enables the use of else statement with the while loop. The else block is executed when the
condition given in the while statement becomes false.
Looping Statements in Python 87

