Python while Loop Statements
While loop is used in Python to repeat a block of code until the condition in while statement is true. It is also used quite frequently in Python.
The syntax of while in python is given below.
i=1; while i<=5: print(i); i=i+1;
while loop with else
We can use an optional block with a while loop that will execute once the loop is terminated. But one thing to notice is that, if the loop contains any loop control statement ( i.e. break, continue or pass ), then the else block is not executed.
i = 1 while i<=5: print(i) i = i + 1 else: print("Loop terminated")
2
3
4
5
Loop terminated