Use else statement in loops
You can use the else statement in the for and while loops in Python. The else statement is optional and executes if the loop iteration completes normally. If the loop is terminated with a break statement, the else statement will not be executed.
flowchart
syntax 1
For <variable> in <sequence>:
statement 1
statement 2
:
else:
statement(s)
syntax 2
while<test condition>:
statement 1
statement 2
:
else :
statement (s)
Example 1
for a in range(1,4)
print("Element is ",end=' ')
print(a)
else :
Print("Ending loop after printing all element of sequence ")
Example 2
while a<4:
print("Element is ",end=' ')
print(a)
else :
Print("Ending loop after printing all element of sequence ")
No comments:
Post a Comment
for more information please share like comment and subscribe