This is what I mean by a for-while loop:
for (<init>; <update>) <body> while (<test>);
It roughly translates to:
<init>
<body>
while (<test>) {
<update>
<body>
}
This seems like a very common programming pattern. Yet, no language that I know of has such a control structure. Hence, a common workaround is to implement it as follows instead:
<init>
while (true) {
<body>
unless (<test>) break;
<update>
}
While this works, it feels very clunky. In my humble opinion, having a conditional break in the middle of an infinite while loop goes against the spirit of structured programming.
Here's a motivational example for the need of for-while loops:
function swap(arr, i, j) {
var tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
return arr;
}
function heapify(heap, i) {
var len = heap.length;
for (var min = i; swap(heap, i, min), i = min) {
var left = 2 * i + 1;
var right = 2 * (i + 1);
if (left < len && heap[left] < heap[min]) min = left;
if (right < len && heap[right] < heap[min]) min = right;
} while (min !== i);
return heap;
}
As you can see, the control is very easy to follow and the code is a lot cleaner than the workaround:
function swap(arr, i, j) {
var tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
return arr;
}
function heapify(heap, i) {
var len = heap.length;
var min = i;
do (true) {
var left = 2 * i + 1;
var right = 2 * (i + 1);
if (left < len && heap[left] < heap[min]) min = left;
if (right < len && heap[right] < heap[min]) min = right;
if (min === i) break;
swap(heap, i, min);
i = min;
}
return heap;
}
Anyway, do you know of any programming language which has such a control structure? Lisp doesn't count because it's primarily functional and I'm looking for a primarily imperative programming language.