Python while Loop Statements
A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.
Syntax of while
The syntax of a while loop in Python programming language is-
Example : example of while
# example of the while loop
Example : Finding the sum of digits of a number
# finding the sum of digits of a number
number=int(input("Enter the number "))
print("sum of the digits : "+ str(sum))
Enter the number 23242
sum of the digits : 13
str() - function is used to convert other type value to string value
The Infinite Loop
A loop becomes infinite loop if a condition never becomes FALSE. You must use caution when using while loops because of the possibility that this condition never resolves to a FALSE value. This results in a loop that never ends. Such a loop is called an infinite loop.
# Infinite Loop
Traceback (most recent call last):
File "dm.py", line 3, in
While Loop with else block
If the else statement is used with a while loop, the else statement is executed when the condition becomes false.
# While loop with else
What next ?
Python break statement