Issue
It seems both of the below codes are printing the same, then what is the need of "else" block after "for" loop in python.
Code 1:
for i in range(10):
print i
else:
print "after for loop"
Code 2:
for i in range(10):
print i
print "after for loop"
Thanks in advance.
Solution
Loop statements may have an
else
clause; it is executed when the loop terminates through exhaustion of the list (withfor
) or when the condition becomes false (withwhile
), but not when the loop is terminated by abreak
statement.
Follow the link for an example how this can be used.
Answered By - Aaron Digulla Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.