# Simple for loop question

**URL:** <https://discourse.julialang.org/t/simple-for-loop-question/67775>\
**Category:** New to Julia\
**Tags:** loops\
**Created:** [September 6, 2021, 9:49pm UTC](https://discourse.julialang.org/t/simple-for-loop-question/67775 "2021-09-06T21:49:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![solaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/solaris/32/28875_2.png) [@solaris](https://discourse.julialang.org/u/solaris)\
**Post date:** [September 6, 2021, 9:49pm UTC](https://discourse.julialang.org/t/simple-for-loop-question/67775/1 "2021-09-06T21:49:00Z")

</div>

is there a way to arbitrarily step in a for loop? I don’t see it in the docs. For example:

```julia
for i in 1:1000
       println("msg: ", i)
end

```

but going 2 by 2 or 3 by 3 instead of one at a time. The equivalent of C’s:

```julia
for(i = 0; i < 1000; i += 3) ...

```

or would I use a while loop in those cases?

---

<div class="post-metadata">

**Author:** ![GHL](https://avatars.discourse-cdn.com/v4/letter/g/7feea3/32.png) [@GHL](https://discourse.julialang.org/u/GHL)\
**Post date:** [September 6, 2021, 9:54pm UTC](https://discourse.julialang.org/t/simple-for-loop-question/67775/2 "2021-09-06T21:54:23Z")

</div>

If I understood you correctly:

```julia
for i in 0:3:1000
       println(i)
end

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 6, 2021, 9:55pm UTC](https://discourse.julialang.org/t/simple-for-loop-question/67775/3 "2021-09-06T21:55:44Z")

</div>

> [@GHL](#):
>
> ```julia
> for i in 0:2:10
> println(i)
> end
> 
> ```

`0:3:10`?

---

<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:** [September 6, 2021, 10:18pm UTC](https://discourse.julialang.org/t/simple-for-loop-question/67775/4 "2021-09-06T22:18:40Z")

</div>

> [@solaris](#):
>
> The equivalent of C’s: `for(i = 0; i < 1000; i += 3) ...`

You could do

```julia
for i in 0:3:999
   ...
end

```

(using `999` for the upper bound to mimic `< 1000`, though in this case `0:3:1000` would have the same effect since `1000` is not a multiple of `3`). See the [`:` and `range` documentation](https://docs.julialang.org/en/v1/base/math/#Base.::) and also [`StepRange`](https://docs.julialang.org/en/v1/base/collections/#Base.StepRange).

Of course, you can also write the loop more explicitly:

```julia
i = 0
while i < 1000
    ...
    i += 3
end

```
