In this Python tutorial, we will study about the Python while loop, how to use while loop in Python, Syntax of while loop and will understand python while loop with example.
While loop is used when we want to execute certain statements until some condition met. When our requirement is to run the specific code unlimited time as long as our condition met, In that case, we use while loop.
General Syntax of while loop: if we talk about general Syntax of Python while loop is
set of statements to execute
So This is Syntax of While loop, where is there with While loop, till that condition is true, set of statements to execute will run.
while loop in Python
Now we will see how we can use while loop in Python.
First, we will see the syntax of Python while loop:
set of statements
This is the general syntax of Python while loop.
Observation from Python while loop syntax:
- While keyword is used here.
- There is colon ( : ) after condition.
- After the colon, There is a set of statements which will execute till while the condition is met.
Python while loop Example
Let’s see Python while loop with an example.
Suppose we have to take input from the user as an only even number (Even Number is not divided by 2) if the user enters an odd number (an odd number is not divided by 2), the program will stop.
so how we can do that in Python
i=int (input("Enter Even no. :")) while i%2==0 : i=int (input("Enter Even no. :")) print("Oops,You enter odd Number Bye!!")
Enter Even no. :2 Enter Even no. :4 Enter Even no. :6 Enter Even no. :8 Enter Even no. :1 Oops,You enter odd Number Bye!!
This is a very basic example just to demonstrate Python while loop.
In Python, an input keyword is used to take input from the user. we will read about input keyword later in detail. Here we will take the first input from the user. If that input number is even only that case python while loop will work.
If the user enters an odd number, python while loop will not execute, as we can see the example input, till the user enter even number (2,4,6,8), while loop work, as soon as user enter odd number (1 in this case), It does not enter in while loop in python.
so we have explained python while loop by taking the basic example.
Use of Break and Continue keyword in While loop in Python
Now, we will learn about these two important keywords in python which we can use in while loop in Python:
Break keyword in while loop
Continue keyword in while loop
Python Break in while loop
First, we will see how python break keyword can be used in while loop Python. As we know that the purpose of the break is to break the loop and control will come to the next line of the loop block.
Example: let’s understand it by example.
We will take the same example, but in this case, the user has to enter any even number except 4. as soon as the user will enter 4, it will break out from the loop.
i=int (input("Enter Even no. :")) while i%2==0 : i=int (input("Enter Even no. :")) i==4: break print("Oops,You enter either 4 or odd Number Bye!!")
Enter Even no. :2 Enter Even no. :8 Enter Even no. :102 Enter Even no. :4 Oops,You enter either 4 or odd Number Bye!!
the program will execute and it will ask to enter the even number, as soon as user enter number 4, a loop will break.
So, we study here how to use the break in while loop in Python.
Python continue while loop
continue is used to skip the current iteration if condition met and continue next.
In Python, we can use continue by taking an example in the following way
Example: we take the same example or use case of continue in python, but now 4 is the invalid number for our program when user will enter 4, we have to tell to the user that “please enter another even number”, so we write code
i=int (input("Enter Even no. :")) while i%2==0 : i=int (input("Enter Even no. :")) if i==4: print("You can not enter 4! please enter another even no.") continue print("Oops,You enter odd Number Bye!!")
Enter Even no. :2 Enter Even no. :6 Enter Even no. :4 You can not enter 4! please enter another even no. Enter Even no. :58 Enter Even no. :5 Oops,You enter odd Number Bye!!
So, here when user enter 4, we are showing error to user that
You can not enter 4! please enter another even no.
So, this was a simple use case or example of continue in python program.