While loop in python

Introduction

A Python ‘while loop’ repeatedly carries out a target statement while the condition is true. The loop iterates as long as the defined condition is true. When the condition becomes false, program control passes to the line immediately following the loop.

Syntax:

while expression:
   statement(s)

Example:

i = 0
while i < 3:
   print (i, " is  less than 3")
   i += 1 # Incrementing i

Note: remember to increment i, or else the loop will continue forever.

Output:

0  is  less than 3
1  is  less than 3
2  is  less than 3

Python interprets any non-zero value as True. None and 0 are interpreted as False.

Example:

while True:
    print("I am a programmer")
    break
while False:
    print("Not printed because while the condition is already False")
    break

Output:

I am a programmer


While loop control statement

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following three control statements in the while loop:

Break statement

A break may only occur syntactically nested in a for or a while loop, but not nested in a function or class definition within that loop. It terminates the nearest enclosing loop, skipping the optional else clause if the loop has one. If a while loop is terminated by a break, the loop control target keeps its current value.

Example:

i = 0
while i < 10:
    print(i)
    if i == 2:
        break
    i += 1

Output:

0
1
2
Continue statement

Continue may only occur syntactically nested in a for or a while loop, but not nested in a function or class definition within that loop. It continues with the next cycle of the nearest enclosing loop.

Example:

i = 0
while i < 5:
  i += 1   
  if i == 3:
      continue
  print(i)

Output:

1
2
4
5
Pass statement

Pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically when no code needs to be executed.

Example(when executed nothing happens):

while True:
    pass  


Else clause in while loop

Python supports having an else statement associated with loops, unlike other programming languages.  When the loop condition of the “for” or “while” statement fails then the “else” statement is executed.

Note: If the break statement is executed inside the loop, then the “else” part is skipped. But not when it is executed with the continued statement.

Syntax:

while expression:
   statement(s)
else:
   statement(s)

If the else statement is used with a while loop, the else statement is executed when the condition becomes false

Example:

count = 0
while count < 3:
   print (count, " is  less than 3")
   count += 1
else:
   print (count, " is not less than 3")

Output:

0  is  less than 3
1  is  less than 3
2  is  less than 3
3  is not less than 3


Nested while loop

Python programming language allows to use of one loop inside another loop and it is called a nested loop.

The syntax for the nested while loop:

while expression:
   while expression:
      statement(s)
   statement(s)

Example:

i = 1
j = 5
while i < 4:
    while j < 8:
        print(i,",", j)
        j = j + 1 # increment j
        i = i + 1 # increment i

Output:

1, 5
2, 6
3, 7