# Julia documentation about FOR loop

**URL:** https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725
**Category:** New to Julia
**Created:** [October 8, 2023, 7:30am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725 "2023-10-08T07:30:41Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![atBNE](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@atBNE](https://discourse.julialang.org/u/atBNE)
#### Post date: [October 8, 2023, 7:30am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/1 "2023-10-08T07:30:41Z")

</div>

Incomplete / missing element in Julia documentation about: Repeated Evaluation: Loops  
[https://docs.julialang.org/en/v1/manual/control-flow/#man-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.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [October 8, 2023, 7:40am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/2 "2023-10-08T07:40:54Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![atBNE](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@atBNE](https://discourse.julialang.org/u/atBNE)
#### Post date: [October 8, 2023, 8:00am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/3 "2023-10-08T08:00:48Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [October 8, 2023, 8:05am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/4 "2023-10-08T08:05:01Z")

</div>

This would be

```julia
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
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

```

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 8, 2023, 8:10am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/5 "2023-10-08T08:10:42Z")

</div>

> [@bertschi](#):
>
> ```julia
> for x in 2:3:17
> @show x
> end
> 
> ```

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

---

<div class="post-metadata">

### Author: ![atBNE](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@atBNE](https://discourse.julialang.org/u/atBNE)
#### Post date: [October 8, 2023, 8:12am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/6 "2023-10-08T08:12:02Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 8, 2023, 8:17am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/7 "2023-10-08T08:17:20Z")

</div>

Maybe here: [Collections and Data Structures · The Julia Language](https://docs.julialang.org/en/v1/base/collections/#Base.StepRange)

Typing `?step` prints:

```julia
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> 

```

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [October 8, 2023, 8:29am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/8 "2023-10-08T08:29:41Z")

</div>

I think that this section of the documentation can be relevant

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

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 8, 2023, 8:38am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/9 "2023-10-08T08:38:18Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)
#### Post date: [October 8, 2023, 9:03am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/10 "2023-10-08T09:03:39Z")

</div>

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:

```julia
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](https://docs.julialang.org/en/v1/base/base/#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](https://docs.julialang.org/en/v1/manual/control-flow/#man-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.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [October 8, 2023, 9:10am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/11 "2023-10-08T09:10:53Z")

</div>

> [@ufechner7](#):
>
> Nevertheless this example could be added to the documentation of for loops…

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:

```julia
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](https://github.com/JuliaLang/julia/issues) and there you can refer to this discussion.

---

<div class="post-metadata">

### Author: ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)
#### Post date: [October 8, 2023, 9:28am UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/12 "2023-10-08T09:28:36Z")

</div>

I filed the [issue](https://github.com/JuliaLang/julia/issues/51638) so that the OP @atBNE (probably a new user) does not have to bother.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 8, 2023, 2:36pm UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/13 "2023-10-08T14:36:42Z")

</div>

> [@ufechner7](#):
>
> Nevertheless this example could be added to the documentation of for loops…

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](https://github.com/JuliaLang/julia/pull/51639)

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [October 8, 2023, 10:46pm UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/14 "2023-10-08T22:46:19Z")

</div>

> [@zdenek\_hurak](#):
>
> 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

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.

---

<div class="post-metadata">

### Author: ![Marco\_Antoniotti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_antoniotti/32/18697_2.png) [@Marco\_Antoniotti](https://discourse.julialang.org/u/Marco_Antoniotti)
#### Post date: [July 13, 2024, 8:28pm UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/15 "2024-07-13T20:28:12Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [July 13, 2024, 8:41pm UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/16 "2024-07-13T20:41:18Z")

</div>

> [@Marco\_Antoniotti](#):
>
> how do you do this?

Maybe you mean,

```julia
for (i,e) in pairs("asdf")
    println(i,' ',e)
end

```

---

<div class="post-metadata">

### Author: ![Marco\_Antoniotti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_antoniotti/32/18697_2.png) [@Marco\_Antoniotti](https://discourse.julialang.org/u/Marco_Antoniotti)
#### Post date: [July 14, 2024, 5:11pm UTC](https://discourse.julialang.org/t/julia-documentation-about-for-loop/104725/17 "2024-07-14T17:11:28Z")

</div>

Nice. Thanks. Not the same, but it solves my problems.

Marco
