Julia documentation about FOR loop

Incomplete / missing element in Julia documentation about: Repeated Evaluation: Loops
https://docs.julialang.org/en/v1/manual/control-flow/#man-loops

The description of the “for” loop is missing the “step” value.
Please can you add this (big) detail to the doc.

1 Like

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?

5 Likes

I was thinking to do something like:
for variable_name to get the value: 2 5 8 11 14 17 etc …

This would be

for x in 2:3:17
    @show x
end

As @Benny already explained, the step is not part of the loop, but instead part of the iterator you are using:

julia> myrange = 2:3:17
2:3:17

julia> typeof(myrange)
StepRange{Int64, Int64}

julia> first(myrange)
2

julia> last(myrange)
17

julia> step(myrange)
3
5 Likes

Nevertheless this example could be added to the documentation of for loops…

6 Likes

Thank you, it is now very clear. I could not find out in the doc any help about this. Do you know where it is explained in the doc?

2 Likes

Maybe here: Collections and Data Structures · The Julia Language

Typing ?step prints:

help?> step
search: step StepRange StepRangeLen CompositeException RoundNearestTiesUp

  step(r)

  Get the step size of an AbstractRange object.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> step(1:10)
  1
  
  julia> step(1:2:10)
  2
  
  julia> step(2.5:0.3:10.9)
  0.3
  
  julia> step(range(2.5, stop=10.9, length=85))
  0.1

julia> 

I think that this section of the documentation can be relevant

https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration

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…

4 Likes

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.

3 Likes

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].

OP, you may open up an issue here Issues · JuliaLang/julia · GitHub and there you can refer to this discussion.

1 Like

I filed the issue so that the OP @atBNE (probably a new user) does not have to bother.

9 Likes

I just created a PR to clarify the docs and include a step-range example: clarify docs on for loops for general iterators by stevengj · Pull Request #51639 · JuliaLang/julia · GitHub

12 Likes

There are:

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.