bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Control Flow Statements – Python While Loop

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 previous module is the “if else statements”. Another one of the control flow statements are 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 loops in Python. Following is the list of all the topics that we will cover this module, in case you need to jump to a specific one.

So, without any further delay, let’s get started.

Learn Python in 16 hrs from experts

What is while loop in Python?

While loop statements in python are used to repeatedly execute a certain statement for as long as the condition provided in the while loop statements stays true. While loop lets the program control to iterate over a block of code.

Syntax of the while loop in Python:

while test_expression:
body of while

The following flowchart explains the working of while loop in Python.
while loop

The program first evaluates the while loop condition, if it’s true then the program enters the loop and executes the body of while loop. It continues to execute the body of while loop for as long as the condition is true. When it is false then 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

Wish to Learn Python? Click Here

Infinite while loop

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.

Following example shows an infinite loop:

a = 1
while a==1:
b = input(“what’s your name?”)
print(“Hi”, b, “, Welcome to Intellipaat!”)

If you run the above code block it will execute an infinite loop which will ask your name again and again. The loop won’t break until you press CTRL+C.
Output:

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
Become Python Certified in 16 hrs.
CLICK HERE

Else with while loop

In Python, we can also use else statement with loops. When else statement is used with 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 following example illustrates using else with 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 while loop till the condition is true, meaning 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 and 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”.

Loop interruptions

Python offers the following two keywords which we use to prematurely terminate a loop iteration.

1. break: 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: 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.

Example:

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

Output:

2
4
5

With this we come to an end of this module, the next module highlights Python for loops, See you there!

Now, you can look at our Python Training to excel your career to the new heights. Also check our free interview questions for Python.

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Python While Loops"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Python While Loops.