Generally in programming loops are used to executed block of code repeatedly as long as condition is true. In this article, you will learn how to use a while loop and while loop with else statement in Python.
"While Loop" is used to run block of code again and again without knowing number of times to repeat. We use this loop when we don't know number of times to iterate. The statement will do repeatedly until condition change to false.
Syntax
while condition :
# to do statement
Example:
num = 5
while num > 0 :
print("countdown",num)
# update num or else infinite loop
num -= 1
print("Happy New Year")
Output:
countdown 5 countdown 4 countdown 3 countdown 2 countdown 1 Happy New Year
Using while loop should double check update counter (num) or write condition to break the loop unless your program will loop forever.
Explain:
Line 2-3: we start launch to check before jump in loop body. The num variable is greater than zero which is true, so it fall into loop body as print "countdown 5" and so on.
Line 5: The num variable subtract by 1 in each time loop repeat. The num continuously minus itself until num is zero then break the loop to print out a line "Happy New Year"
The loop will run infinitely if the condition never turn to false. A program write loops and never end is called an infinite loop or loop forever. Using in wrong way will make the system stuck or not responding. Infinite loop is usually use in server/client programming because the server have to keep running to listen any events that client request (communication between client/server).
Example:
a = 0
# always true
while a==0 :
text = input("Input something: ")
print("You have input ", text)
Output:
Input something: Hello You have input Hello Input something: 18 You have input 18 Input something: world You have input world
Press Ctrl + C for Windows to exit the infinite loop
I hope you have read my article about how to use if-else statment ready. The else statement will only execute when if condition is false. In while loop is work similar concept as well, but not really is, please look at syntax and example below
Syntax
while condition :
# to do statement
else :
# to do when condition false
Example:
num = 5
while num > 0 :
print("countdown",num)
# update num or else infinite loop
num -= 1
else:
print("Happy New Year")
print("Completed task")
Output:
countdown 5 countdown 4 countdown 3 countdown 2 countdown 1 Happy New Year Completed task
Explain:
Line 5: the loop is work normally but after loop until num subtract by 1 and remain 0 it will go up and check the loop again. Is num greater than 0? num now is zero which is false, so the step to run is in else statement which is print out "Happy New Year"
--adres-
We can write while loop inside other while loop statement. The combination help to solve problem in different algorithm.
Example:
x = 1
# first loop
while x < 4:
y = 0
# second loop
while y < 2:
print(x,y)
y+=1
x+=1
Output:
1 0
1 1
2 0
2 1
3 0
3 1
Explain:
In summary hope you understand how single loop works. Once first loop start, second loop run 2 times which mean first loop have 3 loops to run and second loop have 2 loops to run in total there are 3 x 2 =6 loops to run.
Hope you learn and help you to understand basic while loop statement in Python.
PythonAs the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him