Loop structures: Difference between revisions
Content deleted Content added
Simple case first, comments |
|||
Line 747:
===for===
Typically for is used to iterate over items in an iteratable:
for i in range(10):▼
print i▼
else:▼
for x in ["foo", "bar", "baz"]:
print x
It is also used to create indexes:
▲ print numbers[i]
Optional else is executed if break was not use to stop the loop:
for i in numbers:
if item < 0:
break
▲ else:
print 'all numbers are positive'
==[[Ruby]]==
|