Friday 4 December 2015

Python: break statement

'break' statement is used to come out of loop like for, while.


test.py

i = 0

while (1):
 i+=2
 print(i)
 if(i==10):
  break
else:
 print("Exiting loop")
 
print("Finished Execution")

$ python3 test.py
2
4
6
8
10
Finished Execution


As you observe the output, print statement in else clause is not printed. It is because, else clause will not execute, when a break statement terminates the loop.




Previous                                                 Next                                                 Home

No comments:

Post a Comment