Hi @atBNE could you explain a bit more about what you mean?
I suspect you’re thinking of how some languages want the programmer to specify an initial, final, and step value for a for loop, but that’s not how it works in julia.
for loops in julia operate on iterators, and those iterator objects themselves decide what their steps are. e.g. 1:3 is a UnitRange which counts from 1 to 3 in units of 1. The array ["foo","bar","baz"] can be treated as an iterator which produces 3 values before it’s finished: "foo", "bar", and "baz".
Does that help? Or did you have a different question in mind?
Well, this is more about creating iterators. Beginners only need to know how to use them. I never created one even though I am using Julia for 10 years now…
Having checked the documentation, I am inclined to support the view that the for is not sufficiently documented.
The docstring only contains an example of an enumeration-based usage:
help?> for
search: for foreach foldr floor mapfoldr factorial EOFError OverflowError
for
for loops repeatedly evaluate a block of statements while iterating over a
sequence of values.
The iteration variable is always a new variable, even if a variable of the
same name exists in the enclosing scope. Use outer to reuse an existing
local variable for iteration.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> for i in [1, 4, 0]
println(i)
end
1
4
0
Identical entry for the for keyword (what else would a beginner search for) the online manual.
Those who searched for something like control flow would find the section Repeated evaluation: loops. But examples only add the for i = 1:3 example on top of the enumeration-based ones. No discussion of the step choice. No reference to the concept of iterator, to which many are referring in this discussion thread, in this context.
The OPs question is quite reasonable. Searching for a kind of step at the for loop seems to be straight forward for someone coming from another language. Putting this example there is a good idea, together with a note that step 3 is part of the iterator and a link to the documentation of the range object/operator (it seems documentation misses a section about range operator : , i didn’t found it). The REPL help is good:
help?> :
search: : :: ?: >: <:
(:)(start, [step], stop)
Range operator. a:b constructs a range from a to b with a step size of 1 (often a UnitRange), and a:s:b is similar
but uses a step size of s (a StepRange or StepRangeLen). See also range for more control.
The operator : is also used in indexing to select whole dimensions, e.g. in A[:, 1].
: is also used to quote code, e.g. :(x + y) isa Expr and :x isa Symbol. Since :2 isa Int, it does not create a range
in indexing: v[:2] == v[2] != v[begin:2].
Here the 1:3 is a range object, representing the sequence of numbers 1, 2, 3.
In general, the for loop construct can iterate over any container.
Various types of iterable containers will be introduced and discussed in later sections of the manual
Multiple nested for loops can be combined into a single outer loop, forming the cartesian product of its iterables:
Though I agree that repeating 1 unambiguously defined term instead of “range object…containers…iterable containers…iterables” is a lot more useful to people learning the language. In this specific case it would be worth clarifying if iterators and iterables are synonyms or have a distinction; the term “iterables” is used more often but I see “iterators” used synonymously sometimes. In some languages like Python they are distinct.