Page 49 - tp_Modula_v2.0
P. 49
Clickipedia
Any non-zero value in the while loop indicates always a true condition, whereas zero indicates
always-false condition.
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.
Program 6: To demonstrate the use of while loop with else statement.
Program6.py
File Edit Format Run Options Window Help
i = 1
while i <= 5:
print(i)
i = i + 1
else:
print('The while loop ends here')
On running the above program, you will get the following output:
Output
1
2
3
4
5
The while loop ends here
Looping Statements in Python 47

