When you want to run your code repetitively on a particular condition, you can use python's while loop. While Loop in python is similar to that of other languages. It contains a condition and the loop body. The body of the loop executes continuously until the condition is true.
The term Loop is used here because the control returns to the while statement, again and again, creating a loop. As the condition becomes false, the loop breaks and the control goes to the statement next to the loop body.
while condition :
body of loop
- counter: While loop needs a counter which is incremented to run the loop. This counter is a variable which is usually set to 1.
- while keyword
- condition: The condition may be a comparison, boolean or logical statement which is tested at the beginning of every iteration.
- body of loop: It contains a set of instructions. The body of the loop must be indented.
Flow of control:
Let us create a program using while loop.
This program will print your name for the desired number of times.
name = str(input("Please enter your name : ")) max = int(input("Please enter the number of times you want to your name : ")) i=1 while i<=max: print (i,".",name) i=i+1 print("Thank you!")
Please enter your name : Kabir Please enter the number of times you want to your name : 7 1 . Kabir 2 . Kabir 3 . Kabir 4 . Kabir 5 . Kabir 6 . Kabir 7 . Kabir Thank you!
- In the above program, a variable name is defined which takes a string from the user and another variable max is defined which takes an integer from the user.
- One more variable i is defined which refers to 1. It will be used as a counter for the while loop in the program.
- The next line begins with the while keyword followed by a condition (i<=max). This condition will cause the compiler to print the string for max number of times. As the value of i will exceed max, the loop will terminate.
- As per the input given by the user, max = 7 and the value of i is set to 1. Therefore, the condition evaluates to true and the loop body is executed.
- There are two instructions in the loop body, the first instruction prints the value of the variable i with the value of the string name & the second instruction increments the value of i by 1.
- Since it is a loop, the control goes back to while statement and the condition is evaluated. This process continues till the condition is true ie (i = 7).
- As the value of i is incremented to 8, the condition becomes false and the control exits the loop body and goes to the instruction next to loop body and prints a message saying 'Thank you'.
Take a look at this table to understand the working of the Python while loop.
While loop with else-block
Python provides an optional else-block with the while loop. When the condition becomes false, the else-block is executed. If the while loop has a break statement, else-block is skipped.
m = int(input("Enter a number: ")) i=1 while i<=m: print(i) i+=1 else: print("Loop was instructed to run for",m,"times only.")
Enter a number: 5 1 2 3 4 5 Loop was instructed to run for 5 times only.
A Situation : Infinite Loop
It is very important to modify the counter in a loop. If you forget to increment/decrement the value of the counter, you may get stuck in an infinite loop.
Let us see how.
Here, we are going to comment the instruction which is incrementing the value of i in the above program.
name = str(input("Please enter your name : ")) max = int(input("Please enter the number of times you want to your name : ")) i=1 while i<=max: print (i,".",name) #i=i+1 print("Thank you!")
Please enter your name : Vishal Please enter the number of times you want to your name : 5 1 . Vishal 1 . Vishal 1 . Vishal 1 . Vishal 1 . Vishal 1 . Vishal 1 . Vishal . . . Traceback (most recent call last): File "C:/Users/TS7/AppData/Local/Programs/Python/Python37/while.py", line 5, in <module> print (i,".",name) KeyboardInterrupt
- Look at the output of the program, it prints '1. Vishal' infinitely. Since there is no change in the value of i, the condition becomes true forever ie (1 <= 5). Thus, it is very important to make the condition false to stop the loop.
- To interrupt the infinite loop, you need to press 'Ctrl + c'. This will terminate the loop.