Pre-requisite : while loop in Python
while-else loop in Python contains else statement specified after while loop. In while-else loop, a block of code specified under else statement is executed once the loop ends.
while(condition) Statements else: Statements to be executed after loop
Let us check the working of while-else loop with the help of an example :
Program : Print following series : 1, 4, 7, 10, 13, “Inside else” using while-else loop.
# while else Illustration element = 1 while(element<=13): print(element, end=' ') element+=3 else: print("Inside else")
This loop runs 5 times and gets terminated after element > 13; Once terminated, statements under else block are executed. This is how while-else works in Python.
Do you know ?
Python does not supports anything like while-elif statement.
That’s all, see you in the next lecture.