3 ms·
While I understand that it's mostly a question of aesthetics, I don't like this also because it might break the expected semantics of continue/break in such a l
by cnvogel 11y ago
While I understand that it's mostly a question of aesthetics, I don't like this also because it might break the expected semantics of continue/break in such a loop. When I iterate over things more complex than a single variable where the conditions in a for() would become ugly, I tend to put the domain-specific "increment" into a small static function:
static int
next_pixel(*x, *y, width, height) {
(*x)++;
...
if (last pixel...)
return 0;
return 1;
}
/* later on... */
do {
/* do something with coordinates x, y */
} while (next_pixel(&x, &y, WIDTH, HEIGHT));
This is, of course, not only restricted to 2D arrays, but could cover walking of a tree or a filesystem...