There are two kinds of loops in Cython, for...in and for...from. This describes their specified (perhaps not yet implemented) behavior.
for i in L:
...works just like python. Specifically, the variable i is not assigned to if L is empty, and is last assigned to the last value of L. If i is assigned to in the loop, the next value of i is not affected.
for i in range(...):
...may be optimized to a C for loop if i is a cdef int, but the semantics will remain exactly the same. This is accomplished by using a temporary variable for the actual C loop.
for i from expr1 <= i < expr2 [by expr3]: ...
gets translated to
start = expr1;
end = expr2;
step = expr3;
for(i=start; i<end; i+=step) {
...
}In particular:
- modifying the variable from within the loop will alter its execution, (even if it's a Python variable)
- if step=1, after the loop, i == end (while with the for-in loop, i==end-1 after the loop)
In all cases, the bounds and step are evaluated exactly once.
