How to play with the While loop in python

Content

  • Introduction
  • Flow chart of while loop
  • The basic program of while loop
  • Anatomy of a while loop
  • Loop else Statement
  • The Break statement in while loop
  • Continue statement in while loop
  • Conclusion

Introduction

A while loop is also similar to the video loop, if you watch a youtube video you see there's a button of the loop, that loop button repeats the video again and again. 
while loop is also similar to that youtube button it can also repeat the process again and again, it can stop when the condition becomes false.
The general form of while loop in python is
  
  
while <logicalexpression>:
    loop-body

Flow chart of while loop

python while loop




The flow chat show, how the while loop work. When the loop starts it first goes to the condition statements and check itself, if it becomes true then it repeats the process again. And if the condition makes the value of the while loop false then it becomes false and it causes the while loop false.

The basic python program of while loop

To understand the while loop, consider the following code.

Program:

    

  
  
a = 5
while a > 0:
    print ("hello", a)
    a = a-3

Output:
hello 5
hello 2

Know some interesting things about the while loop

* In a while loop, a loop control variable should be initialized before the loop begins as an uninitialized variable cannot be used in an expression.

Code:

    

  
  
while a!=3:
    ......

This created the problem if we don't create the value of p.

* The loop variable must be updated inside the body-of the while in a way that after some time the test -condition becomes false otherwise the loop will become an endless loop or infinite loop.

Code:

    

  
  
a = 5
while a > 0:
    print ("a")
print ("Thank you God")

The output was endless because the loop variable value remains always 5, as its values not be updated, therefore the loop is endless.

The above code is an endless loop as the value of the loop variable is not being updated inside the loop body, hence it always remains 5 and the loop condition remains always true. 
The corrected form of the above loop will be:

    

  
  
  
 a=5
while a > 0:
    print (a)
    a- = 1
print ("Thank")

Loop else Statement

Both loops of python ( for loop and while loop) have an else clause, which is different form else of if-else statements. The else of a loop executes only when the lop ends normally. You'll learn in the next section that if a break statement is reached in the loop, while loop is terminated pre-maturely even if the test-condition is still true.

In order to fully understand the working of the loop-else clause, it will be needed to know about  working of break statements. Thus let us first talk about the break statement and then we'll get back to the loop-else clause again with an example.

The Break statement 

The break statements help to stop the output or line in-between. It can break the while loop.
The general code of the break statements in while loop is:
Code:   

  
  
While <condiation>:
    statements 1
    if  <condition>:
        break
    statements 2
    statements 3
statements 4
statements 5

Infinite Loops and break Statements

Sometimes, the while loop goes to infinite and it never stops because the condition always remains true. To stop the infinite loop we use the break statements.
To understand the  infinite loops with break statements, we will make a simple program of python:
Code:

    

  
  
a = 2
while True:
    print (a)
    a*=2
    if a>100:
       break

The Continue Statements

The continued statements are another jump statement like the break statement as both the statements skip over the code. But the continued statement is somewhat different form break.
The work of the continue statements is to skip the code or output from the output, To understand the continue statements we, will make a simple python program.
Code:

    

  
  
i = 1
while i < 7:
  i += 1
  if i == 5:
    continue
  print(i)
Output:
2
3
4
6
7

Conclusion

In this post, we will learn while loop and different functions of the while loop, and we will also make some programs to learn while loop. And we will also see, how the while loop work and how we will play with the while loop.
Next Post Previous Post
No Comment
Add Comment
comment url