In this article you will learn about Python while loop.
Python while loop – Before moving ahead, let’s know a little bit about Python if else Statement
Python loop – Loop in Python, is built-in function of Python. It carries various types of loop and that helps Python to iterate through elements and get every element separately one-by-one.
While loop – It is used to repeat set of statement until its true. In other words, while loop keeps continue executing a statement until a condition or block of code is true, as condition goes false, it breaks loop and come out of loop.
Example 1- It returns number 1 to 5 until statement is not false. Printing number first then adding number 1 to x’s value.
x = 1 while x < 6: print(x) x = x+1

Explanation – In this example, until x is equal to 5, loop will execute ‘print statement’ because condition is true and at each step it will add 1 to x but as loop reaches to 6, it will exit loop because condition will be false.
Note: Remember to increment x, otherwise the loop will never stop.
Example 2- It returns number 1 to 6 until statement is not false. First adding number 1 to x’s value then printing number.
x = 0 while x < 6: x+=1 print(x)

Explanation – In this example, until x is equal to 5, loop will execute ‘print statement’ because condition is true and at each step it will add 1 to x but as loop reaches to 6, it will exit loop because condition will be false.
Note: Remember to increment x, otherwise the loop will never stop.
Difference in example 1 and 2 – In both example condition (while x < 6) will be checked after adding number 1 to x (x = x+1) but the difference in example 1 is that, number (print statement) will be executed without adding number 1 to x (x = x+1) therefore, last number will be 5 because when condition will be checked for 5+1 it will make condition (while x < 6) false and finally loop will break.
But in example 2, number (print statement) will be executed after adding number 1 to x (x = x+1) therefore, last number will be 6 because when condition will be checked for 6+1 it will make condition (while x < 6) false and finally loop will break.
break loop – Even condition is true still loop can be stopped by using break keyword.
Example 2- Stop or exit loop as x is equal to 4
x = 1 while x < 6: print(x) if x == 4: break x = x+1

Explanation – In this example, until x is equal to 3, loop will execute ‘print statement’ because condition is true and at each step it will add 1 to x but as loop reaches to 4 it will break loop because of break keyword but it will print as well as number 4 , as print() function is before ‘break’ keyword.
The continue Statement – It is used to stop the current iteration or step, and continue with the next.
Example 3- Continue the statement if x is equal to 4.
x = 1 while x < 5: x = x+1 if x == 4: continue print(x)

Explanation – In this example, loop will keep continue to execute the condition after numbers 4 too as ‘if condition’ is equal to 4 but it will not print number 4 as condition lies at number 4.
The else Statement – It is used when given above all statement is not true.
Example 4- Use of else statement when condition is not true.
x = 1 while x > 2:` print(x) else: print('It is else statement')

Explanation – In this example, Python will skip while condition because it is false and executed ‘else statement’ as it was left last condition.
If you find anything incorrect in above discussed topic and have any further question, please comment down below.
Like us on