Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times. Python programming language provides while, for and nested loops to handle looping requirements, but in this tutorial, we will talk only about while loops.
While loop statement in Python language repeatedly executes a target statement as long as a given condition is true.
While syntax:
The syntax of a while loop in the Python programming language is:
while expression:
statement(s)
Here, statement(s) may be a single statement or a block of statements with OR, AND, XOR, or other logical operations. The condition may be any expression, and true is any non-zero value. The loop iterates the statement while the condition is true.
When the loop condition becomes false, program control passes to the line immediately following the loop.
In Python, all statements indented by the same number of character spaces, after a programming construct, are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.
When the loop condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed. Example:
count = 0
while (count < 10):
print ('Counter is:', count)
count = count + 1
print ("We left while loop!")
Above we specify the terms of the while statement, which are: While the condition variable is less than 10, we will print the count variable out. After printing out the count, we will add 1 to the current count.
The above process will continue until the count equals 10.
In a print Shell this will produce the following result:
Counter is: 0 Counter is: 1 Counter is: 2 Counter is: 3 Counter is: 4 Counter is: 5 Counter is: 6 Counter is: 7 Counter is: 8 Counter is: 9 We left while loop!
This while loop, consisting of print and increment (count+1) statements, they are executed repeatedly until the count reaches 9. With each iteration, the current value of the index count is displayed and then increased by 1. In the end, while statement reaches count 9 and leaves while loop.
Infinite loop:
What is called an infinite loop? The loop becomes infinite when the syntax condition never becomes FALSE. Then you must use caution combination (CTRL+C), because of the possibility that the loop condition never resolves to a FALSE value. This results in a loop that never ends. Example:
import time
count = 1
while count == 1 : # This constructs an infinite loop
time.sleep(1) # This gives some delay
print ("Waiting for CTRL+C")
In a print Shell this will produce the following result:
Waiting for CTRL+C Waiting for CTRL+C Waiting for CTRL+C Waiting for CTRL+C Traceback (most recent call last): File "while.py", line 4, in time.sleep(1) KeyboardInterrupt
The above example goes to infinite loop and you need to use the CTRL+C combination to exit the program.
Now we covered while loop tutorial and learned how to use it