# 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:** 2

<div class="post-metadata">

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

</div>

every block in Julia ends with `end`.

```julia
do
   # something
until condition

```

does not and will make the syntax more complicated.

```julia
do
  # something
  until condition
end

```

seems not much of an improvement over

```julia
while true
  # something
  condition && break
end

```

---

<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, 1:37pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/22 "2019-06-05T13:37:52Z")

</div>

I already agreed to try `while true` further up in the discussion, so we really are arguing “semantics” or is it syntax 😁 ? Anyway to me `while true` has always had the feel of a bug like a suspended anvil on a rope above just waiting for me to put a foot wrong. But I will give it the old college try - I might get to like it.

---

<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: [June 5, 2019, 1:38pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/23 "2019-06-05T13:38:19Z")

</div>

That’s not accurate, there’s a list of keywords that end a block: `catch else finally` among them. (I can’t give you the source link as I’m on a phone.) Conceivably, we could add `until` to that list in Julia v2.

---

<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, 1:39pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/24 "2019-06-05T13:39:18Z")

</div>

I actually copied the syntax from Julia’s try-catch block. 🙂

---

<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: [June 5, 2019, 1:43pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/25 "2019-06-05T13:43:38Z")

</div>

> [@tkluck](#):
>
> That’s not accurate, there’s a list of keywords that end a block: `catch else finally` among them

Sure, but all of these start _another_ block, eventually requiring an `end`.

---

<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: [June 5, 2019, 1:53pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/26 "2019-06-05T13:53:18Z")

</div>

> [@ExpandingMan](#):
>
> 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

We should eliminate `while` and introduce

```julia
struct ChasmOfInfiniteNothingness end

Base.iterate(::ChasmOfInfiniteNothingness, _ = nothing) = (nothing, nothing)

```

Example:

```julia
for _ in ChasmOfInfiniteNothingness()
    x = rand()
    if x ≤ 0.1
        @show x
        break
    end
end

```

must… resist… making… PR.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [June 5, 2019, 1:56pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/27 "2019-06-05T13:56:11Z")

</div>

It’d be a useful first project in writing macros to write a @dowhile macro

```julia
@dowhile condition 
    body 
end

```

would transform to

```julia
while(true)
    condvar = begin condition end
    if(condvar) break;
    body
end

```

I’m too new to Julia to figure out how to do this, but I think it’d be pretty easy based on 2 minutes of reading about macros.

---

<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, 2:00pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/28 "2019-06-05T14:00:07Z")

</div>

True but we already did that one: [Could we have a do while loop in Julia? - #9 by rdeits](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/9)

A library solution is not really suitable.

---

<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: [June 5, 2019, 2:27pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/29 "2019-06-05T14:27:46Z")

</div>

> [@dataDiver](#):
>
> A library solution is not really suitable.

I still don’t understand the reasoning behind this, but experimenting in packages before proposing various additions to `Base` and the standard libraries is very common, so if you rule this possibility out from the start then you significantly lower the chances of a change like this being even considered.

Sure, you cannot extend the parser this way, but everything else is possible, particularly with macros. The best solution is to write something super useful in a nice way, package it up, and then at some point if enough people use it the proposal will speak for itself.

---

<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: [June 5, 2019, 2:32pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/30 "2019-06-05T14:32:27Z")

</div>

I guess I’ve only been following this half-heartedly but it’s starting to look to me like people are suggesting things that are entirely equivalent to

```julia
while true
    # stuff here
    condition && break
end

```

but with slightly different syntax. I don’t understand why this is desirable, is the above really so bad? I appreciate the point that alternate syntax is sometimes desirable, but this seems like a case where the existing syntax is simple, elegant and general.

---

<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, 2:42pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/31 "2019-06-05T14:42:21Z")

</div>

Code I write goes into production. I’m pretty conservative about loading packages - they are loaded for functional need not syntactic sugar, it is not good practice (unless the syntactic sugar becomes essentially functional). Something like this is a pretty fundamental language change that should not really be done on the package level.

It’s the kind of thing I’d test out in a script for different use cases and leave it at that.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [June 5, 2019, 4:57pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/32 "2019-06-05T16:57:42Z")

</div>

@dataDiver, each language has its own aesthetic, Julia is in my opinion heavily influenced by Common Lisp, and in Common Lisp macros are everywhere and you just have to like it. Keeping the base language small is a choice that makes for good extensibility.

I think a package that specifically contains a lot of common idiom macros is _exacty_ what’s needed here. Like some sort of UtilityMacros.jl

The alternative is to pour everything into the base language like Perl did, and I specifically find Julia exciting because it isn’t like Perl 😉

---

<div class="post-metadata">

### Author: ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)
#### Post date: [June 5, 2019, 5:07pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/33 "2019-06-05T17:07:02Z")

</div>

I have to agree with @dlakelan, I think one of the whole points of Julia’s design is the goal of first class status for user code relative to base Julia. Macros seems like a key part of that and this situation seems like an obvious place for a macro for those who really want a do while loop.

---

<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: [June 5, 2019, 6:24pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/34 "2019-06-05T18:24:58Z")

</div>

> [@dataDiver](#):
>
> Anyway to me `while true` has always had the feel of a bug like a suspended anvil on a rope above just waiting for me to put a foot wrong.

It seems to me that

```julia
do

```

is _exactly_ and _completely_ identical to

```julia
while true

```

and that

```julia
until condition

```

is indistinguishable from

```julia
condition && break

```

How is it that one is more like a suspended anvil than the other?

I even think the current syntax is cleaner, and less special-case-y.

---

<div class="post-metadata">

### Author: ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)
#### Post date: [June 5, 2019, 6:29pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/35 "2019-06-05T18:29:58Z")

</div>

This is pretty much what Stefan said, but just to spell it out, I find that a surprising number of times I actually want the termination condition somewhere in the _middle_ of the loop. Here’s a random example from Base that I found immediately:

```julia
    while true
        n1 = read(f, Int32)
        totbytes -= 4
        n1 == 0 && break
        skip(f, n1)
        totbytes -= n1
    end

```

So I’m not necessarily 100% against a do-while loop, but it seems like a lot of syntax to spend on a somewhat-arbitrary special case. In fact it doesn’t seem so crazy to me to entirely replace `while` with `repeat ... end`, equivalent to a `while true` loop. Then no condition position gets to be special. `while cond` is just really traditional.

I’ll also add that, given the syntax we have today, writing code twice (to do one iteration outside the loop) is more of a bug factory than `while true`. It’s all too easy to forget to update one of the copies of the code.

---

<div class="post-metadata">

### Author: ![madprogrammer](https://avatars.discourse-cdn.com/v4/letter/m/6de8d8/32.png) [@madprogrammer](https://discourse.julialang.org/u/madprogrammer)
#### Post date: [June 6, 2019, 12:06pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/36 "2019-06-06T12:06:12Z")

</div>

I support the idea that `while true` and `break` is not a great idea, as it does not follow the good programming practices nor clean coding.

That said, Julia main goal is to be fast. Thinking about C language for example, using a lot of breaks on long loops may lead to a slower IPC. Julia does a great job optimizing code, so we have less issues about performance here.

So in the end, we need to think about the main focus of the language: performance or clean coding.

---

<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: [June 6, 2019, 12:14pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/37 "2019-06-06T12:14:39Z")

</div>

> [@madprogrammer](#):
>
> `while true` and `break` is not a great idea, as it does not follow the good programming practices nor clean coding.

Funny you should say this. I searched my edition of the Robert C Martin _Clean Code Collection_, and found multiple examples for recommended code, eg in Appendix A (Client Based Locking):

```C
IntegerIterator iterator = new IntegerIterator();

while (true) {
  int nextValue;
  synchronized (iterator) {
     if (!iterator.hasNext())
       break;
     nextValue = iterator.next();
  }
  doSometingWith(nextValue);
}

```

(sorry if the indentation was mangled with copy-paste).

---

<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: [June 6, 2019, 1:59pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/38 "2019-06-06T13:59:16Z")

</div>

> [@madprogrammer](#):
>
> I support the idea that `while true` and `break` is not a great idea, as it does not follow the good programming practices nor clean coding.

As mentioned in my previous post, it seems to me that `do until` is identical to `while true break`. What am I missing?

---

<div class="post-metadata">

### Author: ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)
#### Post date: [June 6, 2019, 2:38pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/39 "2019-06-06T14:38:45Z")

</div>

Maybe we should remove all loops and replace them by macros, this way `while` is not privileged over `@dountil` and nobody can complain about their missing loop construct. Also, I think that is what the compiler does anyways 🙂

```julia
macro repeat(body)
    quote
        @label start
        $body
        @goto start
        @label break
    end |> esc
end
macro while(cond, body)
    quote
        @label start
        if $cond
            $body
            @goto start
        end
        @label break
    end |> esc
end
macro dountil(body, cond)
...

```

This code is for illustration purposes only and does not work because it uses keywords,

---

<div class="post-metadata">

### Author: ![madprogrammer](https://avatars.discourse-cdn.com/v4/letter/m/6de8d8/32.png) [@madprogrammer](https://discourse.julialang.org/u/madprogrammer)
#### Post date: [June 6, 2019, 2:55pm UTC](https://discourse.julialang.org/t/could-we-have-a-do-while-loop-in-julia/24795/40 "2019-06-06T14:55:10Z")

</div>

I see that on some cases it can be useful (or the only option available), but it doesn’t make it easy to read. Just imagine a larger loop or multiple breaks.

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

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