As discussed in the previous module, we know that Python, like other programming languages, consists of some control flow statements. One of the control flow statements that we have already studied about in the previous module is the Python if else statement. Another one of the control flow statements is loops. Loops are used when we want to repeat a block of code a number of times. In this module, we will learn about the while loop in Python.
Following is the list of all topics that we will cover in this module.
So, without any further delay, let’s get started.
While loop statements in Python are used to repeatedly execute a certain statement as long as the condition provided in the while loop statement stays true. While loops let the program control to iterate over a block of code.
Syntax of While Loop in Python:
while test_expression: body of while
The following flowchart explains the working of while loop in Python.
The program first evaluates the while loop condition. If it’s true, then the program enters the loop and executes the body of the while loop. It continues to execute the body of the while loop as long as the condition is true. When it is false, the program comes out of the loop and stops repeating the body of the while loop.
Let’s see the following example to understand it better.
a = 1 while( a<10): print(” loop entered”, a, “times”) a = a+1 print(“loop ends here”) Output: loop entered 1 times loop entered 2 times loop entered 3 times loop entered 4 times loop entered 5 times loop entered 6 times loop entered 7 times loop entered 8 times loop entered 9 times loop ends here
Infinite while loop refers to a while loop where the while condition never becomes false. When a condition never becomes false, the program enters the loop and keeps repeating that same block of code over and over again, and the loop never ends.
The following example shows an infinite loop:
a = 1 while a==1: b = input(“what’s your name?”) print(“Hi”, b, “, Welcome to Intellipaat!”)
If we run the above code block, it will execute an infinite loop which will ask for our names again and again. The loop won’t break until we press ‘Ctrl+C’.
what’s your name? Akanksha Rana #user input Hi Akanksha Rana , Welcome to Intellipaat! what’s your name? Amrit #user input Hi Amrit , Welcome to Intellipaat! what’s your name? Shubham #user input Hi Shubham , Welcome to Intellipaat! what’s your name? Traceback (most recent call last): #Stopped the loop by entering CTRL+C File “”, line 2, in KeyboardInterrupt
In Python, we can also use the else statement with loops. When the else statement is used with the while loop, it is executed only if the condition becomes false.
a = 1 while a<5: print(“condition is true”) a=a+1 else: print(“condition is false now”) The example illustrates how the else statement works with the while loop. Output: condition is true condition is true condition is true condition is true condition is false now
In the above example, the program keeps executing the body of the while loop till the condition is true, meaning that till the value of a is less than 5. Since the initial value of a is 1 and every time the program entered the loop the value of a is increased by 1, the condition becomes false after the program enters the loop for the fourth time when the value of a is increased from 4 to 5. When the program checks the condition for the fifth time, it executes it as false and goes to the else block and executes the body of else, displaying, ‘condition is false now.’
Python offers following two keywords which we can use to prematurely terminate a loop iteration.
1. Break:The break keyword terminates the loop and transfers the control to the end of the loop.
Example: a = 1 while a <5: a += 1 if a == 3: break print(a) Output: 2
2. Continue:The continue keyword terminates the ongoing iteration and transfers the control to the top of the loop and the loop condition is evaluated again. If the condition is true, then the next iteration takes place.
a = 1 while a <5: a += 1 if a == 3: continue print(a) Output: 2 4 5Previous Next
Download Interview Questions asked by top MNCs in 2019?