Don Clugston:

* Do-while loops, how useful are those actually?

I grepped through the DMD source once, looking for how often Walter uses do..while. The answer: exactly zero.

In my code I use one do-while about every 20 or 30 loops, so they aren't very common, but are useful.

If I need a random 2D point on a disk, one method to produce it is by rejection, I use a do-while loop, I extract two coordinates in a square, and then test if they are inside the disk, otherwise I loop.

But I have a problem with D-style do-while loops, that some time ago I have discussed a bit in D.learn: I can't use them like this, because the variables defined inside the loop are not visible in the loop test:

do {
  // ...
  const x = compute something
} while (predicate(x));


I have to use this, that is less handy:

T x;
do {
  // ...
  x = compute something
} while (predicate(x));

Bye,
bearophile

Reply via email to