In this article you will learn about Python while loop.
Python while loop – Before moving ahead, let’s know about Introduction to Python while loop
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. First, adding number 1 to x, then printing number.
x = 0 while x < 6: print(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 after 5+1, 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, then printing number.
x = 1 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 after 5+1, it will exit loop because condition will be false as 6 will not be less than 6 (while 6 < 6).
Note: Remember to increment x, otherwise the loop will never stop.
Example 3- When while loop not stopped.
x = 1 while x < 6: print(x)
Explanation – In this example, while loop will not stop executing as there is no condition is given to stop while loop.
break loop – Even condition is true still loop can be stopped by using break keyword.
Example 1 – Stop or exit loop as x is equal to 4
x = 1 while x < 6: x+=1 print(x) if x == 4: break
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 number 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.
Example 2 – Stop or exit loop as x is equal to 4
x = 1 while x < 6: if x == 4: break print(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 number 1 to x but as loop reaches to 4 it will break loop because of break keyword but it will not print number 4 , as print() function is after ‘break’ keyword.
Example 3- Execution failed as number 1 is not added to x.
x = 1 while x < 6: if x == 4: break print(x) x+=1
Explanation – In this example, nothing will execute as number 1 can’t be equal to condition ‘if x == 4’ and therefore Python will ignore ‘if statement’ and as well as all lines inside it. So, every time counting will start from number 1 and as ‘if statement’ will be false therefore Python will never reach to print() function to execute number.
continue Statement – It is used to stop the current iteration or step, and continue with the next.
Example 1- Continue the statement if x is equal to 4.
x = 1 while x < 6: x+=1 if x == 4: continue print(x)
Explanation – In this example, loop will keep continue to execute the condition after number 4 too as ‘if condition’ is equal to 4 but it will not print number 4 as condition lies at number 4.
Example 2- Execution failed as number 1 is not added to x.
x = 1 while x < 6: if x == 4: continue x+=1 print(x)
Explanation – In this example, nothing will execute as number 1 can’t be equal to condition ‘if x == 4’ and therefore Python will ignore ‘if statement’ and as well as all lines inside it. So, every time counting will start from number 1 and as ‘if statement’ will be false therefore Python will never reach to print() function to execute number.
else Statement – It is used when given above all statement is not true.
Example – Syntax error with else statement when condition is not true.
x = 1 while x > 2: print(x) else: print('It is else statement') else: print('It is also else statement')
Explanation – In this example, nothing is executed despite while condition is false because two ‘else condition’ is presented here and according to rule more than one ‘else condition’ cannot be used at the same time.
If you find anything incorrect in above discussed topic and have any further question, please comment down below.
Like us on