Tutorial - Python 3 basics
While loop

Posted October 02, 2018 by Rokas Balsys



Programming languages provide various control structures that allow far 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 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 statement while the condition is true.
When 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 loop condition is tested and the result is false, the loop body will be skipped and 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.

Above process will continue until count equals to 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 count reaches 9. With each iteration, the current value of the index count is displayed and then increased by 1. At the end while statement reaches count 9 and leaves while loop.


Infinite loop:

What is called infinite loop? Loop becomes infinite when syntax condition never becomes FALSE. Then you must use caution combination (CTRL+C), because of the possibility that 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

Above example goes to infinite loop and you need to use CTRL+C combination to exit the program.


Now we covered while loop tutorial and learned how to use it. Now you can move to next tutorial.