# Could we have a do while loop in Julia?

**URL:** <https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795>\
**Category:** Internals & Design\
**Created:** [May 31, 2019, 8:55am UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795 "2019-05-31T08:55:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dataDiver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datadiver/32/8174_2.png) [@dataDiver](https://discourse.julialang.org/u/dataDiver)\
**Post date:** [May 31, 2019, 8:55am UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/1 "2019-05-31T08:55:43Z")

</div>

Hi all,

I am having a GREAT time writing Julia code! I write a lot of machine learning code that requires some kind of optimisation/decent and so on and I am missing the do-while construct. Most code I write requires the calculation to be done once before it hits a condition, so I end up kind of executing the code once outside a while loop before entering the loop or doing some other kind of kludge, which is unfortunate given how lovely and clean everything else is 🙂

I think the language would really benefit from adding a do-while loop given that its main focus is writing numerical algorithms.

Thank you kindly. 😉

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 31, 2019, 9:04am UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/2 "2019-05-31T09:04:46Z")

</div>

I’m not familiar with do-while loops, but are they significantly different from a pattern like this:

```julia
condition = true
while condition
    result = process()
    condition = foo(result)
end

```

?

Can you give an example (preferably simple) that illustrates a usecase?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 31, 2019, 9:21am UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/3 "2019-05-31T09:21:15Z")

</div>

or

```julia
while true
    stop_condition() && break
end

```

etc.

---

<div class="post-metadata">

**Author:** ![dataDiver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datadiver/32/8174_2.png) [@dataDiver](https://discourse.julialang.org/u/dataDiver)\
**Post date:** [May 31, 2019, 9:29am UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/4 "2019-05-31T09:29:52Z")

</div>

@DNF Good point!

I usually just write something like

```julia
#... do calc ...
while calcError(...) > epsilon
  # ... do calc ...
end

```

You still have to create the `condition` variable and make sure that it is correctly updated inside the body of your calculation. I guess the do-while isn’t so essential, and I got a little “code blind”, but still a nice to have I think.

---

<div class="post-metadata">

**Author:** ![dataDiver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datadiver/32/8174_2.png) [@dataDiver](https://discourse.julialang.org/u/dataDiver)\
**Post date:** [May 31, 2019, 9:31am UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/5 "2019-05-31T09:31:23Z")

</div>

I avoid using `while true`, seems like a bug waiting to happen.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 31, 2019, 9:35am UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/6 "2019-05-31T09:35:49Z")

</div>

I don’t quite see why.

In any case, what you are asking for is possible with existing control flow constructs, so there is no need for a new one.

---

<div class="post-metadata">

**Author:** ![dataDiver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datadiver/32/8174_2.png) [@dataDiver](https://discourse.julialang.org/u/dataDiver)\
**Post date:** [May 31, 2019, 9:40am UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/7 "2019-05-31T09:40:28Z")

</div>

You’re separating the condition you care about from where it’s actually supposed to be set. It means if anyone messes with that condition or you forget what it’s for, you break your code. It’s different from the conditional break which is clear and obvious, it’s the main loop which I don’t like to mess with.

To me `while true` is a bug, I don’t like to write them.

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [May 31, 2019, 9:49am UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/8 "2019-05-31T09:49:55Z")

</div>

I for one am massively in favour of extending the set of control flow constructs.

The thing with replies like “but you can already do it, here’s how” is that no-one is actually doubting that julia is turing-complete – it’s just that sometimes a certain control construct describes your intention better than another. After all, we do have `for` even though it can be expressed as `while`, and we have `while` even though it can be expressed as `@goto`. (In fact, by the time you get to `while true`, you are at `@goto` levels of expressiveness in my opinion.)

The better you can communicate your intention to whoever reads your code (a reviewer, a contributor, or yourself by the time you forgot what you were thinking when you wrote this), the smoother the development process.

Having said that, unfortunately, I don’t think we can easily create a `do`/`while` construct for parsing reasons (`while` doesn’t close a block; in fact, we want to allow nesting it). And introducing a new keyword (e.g. `do`/`until`) can’t happen until julia 2.0. So I don’t see a particular implementation of this happening any time soon, if at all.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 31, 2019, 12:01pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/9 "2019-05-31T12:01:49Z")

</div>

It’s pretty easy to create a macro to give you almost exactly the syntax you’re looking for. For example: [Some useful macros for Julia · GitHub](https://gist.github.com/MikeInnes/8299575) (see `@once_then`) or: [Introducing Julia/Metaprogramming - Wikibooks, open books for an open world](https://en.m.wikibooks.org/wiki/Introducing_Julia/Metaprogramming) (see `@until`, which is a different structure but illustrates the basic idea).

---

<div class="post-metadata">

**Author:** ![dataDiver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datadiver/32/8174_2.png) [@dataDiver](https://discourse.julialang.org/u/dataDiver)\
**Post date:** [May 31, 2019, 12:09pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/10 "2019-05-31T12:09:29Z")

</div>

Yeah, but that’s a dependency I have to keep track of. It’s a fairly basic thing that doesn’t ship with Julia. I’d rather live with the current situation and hope that it changes than load a “juliakludge” library every time I write code.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 31, 2019, 12:33pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/11 "2019-05-31T12:33:06Z")

</div>

> [@tkluck](#):
>
> The better you can communicate your intention to whoever reads your code (a reviewer, a contributor, or yourself by the time you forgot what you were thinking when you wrote this), the smoother the development process.

In general I agree with this, but

```julia
while true
   ...
end

```

is so common that it can be considered a standard idiom, where the reader expects to look for a breaking condition, assuming that implementing an infinite loop was not the intention.

Simple grepping in Julia sources shows 250+ instances of `while true`.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 31, 2019, 12:37pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/12 "2019-05-31T12:37:15Z")

</div>

> [@dataDiver](#):
>
> I’d rather live with the current situation and hope that it changes than load a “juliakludge” library every time I write code.

Since using a package has such a low cost, IMO the willingness to consider a package for something is a good test of whether you seriously need it.

---

<div class="post-metadata">

**Author:** ![dataDiver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datadiver/32/8174_2.png) [@dataDiver](https://discourse.julialang.org/u/dataDiver)\
**Post date:** [May 31, 2019, 12:46pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/13 "2019-05-31T12:46:15Z")

</div>

A package is an _operational_ need - I generally try to use as few as possible. Syntax is quality of life, nicer code to write/read and so on … I think I made it clear at the start.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 31, 2019, 12:59pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/14 "2019-05-31T12:59:04Z")

</div>

I would suggest embracing `while true`. Almost every while loop I write starts out as a while true loop. Why? Because I generally don’t know what the exit condition is or where I’m going to exit from when I start writing a loop. I can write any loop as a while true with break calls to exit. Then if I notice that one of the exits ends up as the very first thing in the loop—or maybe the very last with the ability to move to the beginning without loss of generality—I’ll turn it into a while cond loop.

---

<div class="post-metadata">

**Author:** ![dataDiver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datadiver/32/8174_2.png) [@dataDiver](https://discourse.julialang.org/u/dataDiver)\
**Post date:** [May 31, 2019, 3:58pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/15 "2019-05-31T15:58:31Z")

</div>

Okay, since it’s you I’ll definitely give it a try.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 31, 2019, 4:10pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/16 "2019-05-31T16:10:39Z")

</div>

Give it a try! Once I started doing this I found it very liberating. Otherwise I would get stuck in “while condition paralysis”—trying to figure out what the condition needs to be before I really had enough information to do so. Instead, now I just write `while true` and get into the meat of the loop and figure out the exit points when I get to them.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 31, 2019, 4:45pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/17 "2019-05-31T16:45:48Z")

</div>

While I don’t think it’s such a bad thing and I wouldn’t be afraid to use it if I really thought it was appropriate, I will admit that `while true` makes me just a little nervous. That said, I so rarely find myself unable to write a `for` loop, even if those `for` loops have `break` statements in them.

---

<div class="post-metadata">

**Author:** ![yukai.yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yukai.yang/32/8757_2.png) [@yukai.yang](https://discourse.julialang.org/u/yukai.yang)\
**Post date:** [June 5, 2019, 11:56am UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/18 "2019-06-05T11:56:11Z")

</div>

First, I think this is a good suggestion. I totally understand your feeling, since you are writing something like

```julia
#... do calc ...
while condition
  # ... do calc ...
end

```

do while is really a grammar sugar or syntactic sugar when

```julia
#... do calc ...

```

has many lines. Definitely you do not want to duplicate them.

However, since there is no do while in Julia so far, I think

```julia
while true
  # ... do calc ...
  if condition
    break
end

```

is one of the best choices… It is not a bug, as it is actually kind of standard for professional programmers…, for example, the “listening” mechanism…

The key point is that the loop should break when the condition is met.

But I support your suggestion. Personally I like the grammar sugar “do while”.

---

<div class="post-metadata">

**Author:** ![dataDiver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datadiver/32/8174_2.png) [@dataDiver](https://discourse.julialang.org/u/dataDiver)\
**Post date:** [June 5, 2019, 12:16pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/19 "2019-06-05T12:16:24Z")

</div>

FYI I usually end up functionalizing `#... do calc ...` for example `doCalc(MyObject, ...)` so before the while loop it’s really just one line; inside the while loop I use the function but other adjustments are made and other things can happen depending on the state of `MyObject`. I end up with something like:

```julia
doCalc(MyObject, ...)
while calcError(MyObject, ...) > epsilon
  doCalc(MyObject, ...)
  # ... maybe do other stuff ...
end

```

I looked at that and thought, it would be nice to do:

```julia
do
  doCalc(MyObject, ...)
  #... maybe do other stuff ...
  until calcError(MyObject, ...) <= epsilon
end 

```

---

<div class="post-metadata">

**Author:** ![yukai.yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yukai.yang/32/8757_2.png) [@yukai.yang](https://discourse.julialang.org/u/yukai.yang)\
**Post date:** [June 5, 2019, 12:39pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/20 "2019-06-05T12:39:56Z")

</div>

that’s absolutely the point and why we prefer the grammar sugar do while.

[Next page](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795.md?page=2)
