The for Loop
The for loop (similar to the for loop in C) repeatedly executes a block of
code. Though it can function more generally like the while loop, it is typically used to run a block of code for each value of a "counter" variable.
The loop is separated into three statements—an initializer, a test statement, and an increment statement.
forexpression,test_expression,increment_expression:code.
The initializer is executed once when the loop starts. It is typically used to set the iteration variable before proceeding. The test statement is run at every iteration to determine whether the loop will continue to execute (similar to the while loop). Finally, the
increment statement is run at every iteration of the loop, typically to update a counter variable. Examples of the for loop
are shown below.
# so, for example, if we have a variable called n (int), this loop will
# print the numbers from 0 to 29.
for n=0, n<30, n+=1: {
print n.
}
# we can also use a different increment statement in order to run the
# loop a bit differently—let's print only even numbers between 2 and 28
for n=2, n<=28, n+=2: {
print n.
}
