Reputation: 55
I found the following for
loop that I cannot understand why it looks different than the traditional one, i.e. (init; condition; increment)
:
int parent, i, indx;
for (; indx; indx = parent) {
parent = (indx - 1) / 2;
if (h->queue[parent] >= value) break;
h->queue[indx] = h->queue[parent];
}
Can someone explain how to convert it to be in the form of (init; condition; increment)
?
Upvotes: 0
Views: 56
Reputation: 1256
for (; indx; indx = parent)
is the standard C for-loop. It simply has a blank (no instructions) for the initialization option.
Upvotes: 1
Reputation: 372992
In a for loop, each of the initialization, termination, and step expressions can be omitted. If the initialization step is skipped, there's no initialization done. If the step is skipped, no step is performed. If the test is skipped, the loop runs until it's broken out of.
Rather than trying to rewrite this loop to include all three expressions, I'd recommend investing the time to learn this syntax, since statements like these aren't all that uncommon.
Upvotes: 2