While Loop–Python

While loop is one of the type of loop in python.In this tutorial,you will learn about while loop syntax,when to use it and how to use it in our programs in different ways.You will also learn about break and continue statement and how to use it with while loop.

When to use while loop?

While loop is used when we don’t know how many times we want to repeat our loop in advance. Although,we can use while loop when we know how many times we want to loop,but to achieve this we can use for loop.Use of while loop makes sense,when we don’t know when to stop our loop.

While loop syntax

while (Condition):
    blocks to execute

while loop check condition each time before executing the block.If condition returns true,then it will execute the block,otherwise loop will break and it will come out of the block.

Using while loop

while loop when we know how many time to loop

If we know in advance,how many times we want to repeat our code,we can use while loop in following ways.

Suppose,we want to print numbers from 1 to 10,then we can do it in following ways.

i=1
while(i<11):
    print(i)

###OUTPUT###
1 1 1 1 1 ---infinite times

Above code will print 1 until you stop it.But,what is wrong?Why its not printing numbers from 1 to 10?I have purposely written the wrong code to show you what you should remember when you are using while loop.

As I have mentioned earlier,while loop is use when we don’t know stopping condition in advance.In condition statement,we are checking if i<11,and if its less than 11,we are printing it.But,there is no stopping condition. Everytime,while loop checks condition, it finds that i<11,and hence condition will be always true and it will print 1.So,how to make it correct?

We need to add a stopping condition,and this can be done by incrementing i inside loop.So,as i will be greater than or equal to 11,condition will be false,and while loop will break.So,whenever you are using while loop,make sure that,at some time condition will become false and while loop will stop executing,otherwise it will go into indefinite loop.So,corrected code is below.

i=1
while(i<11):
    print(i)
    i+=1

##OUTPUT##
1 2 3 4 5 6 7 8 9 10

while loop when we don’t know how many time to loop

Many times we don’t know how many times to loop.In that case,we can use while loop.Let me show you with an example.

Suppose,we want to continuously print whatever user is typing using keyboard.In this case,we surely need to use while loop,because we don’t know how many times,user will type.

while(1): //you can use while(True) also
    string=input("Enter Something")
    print(string)

Above code will ask you to enter something,and after you hit enter,it will print it and again it will ask to enter something.This will keep on going,until you stop the program.

while loop with condition and break statement

We can combine while loop with some condition,and that condition can be used as a breaking point of loop.

Suppose,In above example,we want to stop the loop if user types letter ‘A’ .We can achieve this in following way.

while(1): //you can use while(True) also
    string=input("Enter Something")
    if(string=='A'):
        break
    print(string)

if condition is used to check if input given by user is equals to ‘A’ or not.If input is ‘A’ then break statement will be executed and loop will break and it will come out of the loop.

Problem ,using break statement is that,it comes out of the loop whenever it gets executed.Suppose,we don’t want to break out of loop,and just ignore the letter ‘A’.How we can do it?We can do it using continue statement.

while loop with condition and continue statement

We can combine break and continue keywords in following ways to achieve solution to above problem.

while(1): //you can use while(True) also
     string=input("Enter Something")
     if(string=='A'):
         continue
     print(string)

Above program will print whatever user types.When user types ‘A’,it will ignore ‘A’ and will not print. Continue statement ignores the lines of loop written after it and continues execution of loop from beginning .

while loop with break and continue statement

Suppose,We want to print whatever user types,but we want to ignore ‘A’.If user types ‘Q’ then loop should break and program should exit. We can combine break and continue statement in following way.

while(1): //you can use while(True) also
     string=input("Enter Something")
     if(string=='A'):
         continue
     elif(string=='Q'):
         break
     print(string)

while loop with python list

We can use while loop with python list to iterate over it’s elements.We can do it in following ways.

a=[1,2,3,4,5]
while i < len(a):
    print(a[i])
    i=i+1


   

That’s all for this tutorials.If you have any doubt or suggestion,feel free to comment.

Thank You.

Amarjeet

About Amarjeet

Amarjeet,BE in CS ,love to code in python and passionate about Machine Learning and Data Science. Expertsteaching.com is just a medium to share what I have learned till now with world.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *