How can I write a for
/while
loop inside a #define
directive in C?
We can put a for loop inside a while loop or a do-while loop inside a for loop.
A nested while loop is a while statement inside another while statement. In a nested while loop, one iteration of the outer loop is first executed, after which the inner loop is executed. The execution of the inner loop continues till the condition described in the inner loop is satisfied.
You can put a for loop inside a while, or a while inside a for, or a for inside a for, or a while inside a while. Or you can put a loop inside a loop inside a loop. You can go as far as you want.
do { statement(s); do { statement(s); }while( condition ); }while( condition ); A final note on loop nesting is that you can put any type of loop inside any other type of loop. For example, a 'for' loop can be inside a 'while' loop or vice versa.
Short answer is "don't". But if you have to, for the love of all that's sacred don't do this:
#define FOREACH(start, end) \
for (; (start) < (end); (start)++) \
{ \
// do something interesting \
}
Bad juju all the way around. Note that start
must correspond to an lvalue; you would not be able to call this as FOREACH(1,10)
, or FOREACH((a+b), c)
, or FOREACH(x++,y++)
. All of those would lead to a compile-time error (the operand of ++
must be an lvalue, and none of 1
, a+b
, or x++
qualify). Calling it as FOREACH(x, y++)
will do something you really don't want it to do. Similarly, you wouldn't want to call it as FOREACH(x, y())
.
You can guard against these problems to an extent by doing something like
#define FOREACH(start, end) \
do { \
int i; \
int j = end; \
for (i = start; i < j; i++) { \
// do something interesting \
} \
} while (0)
Essentially, you're creating local variables corresponding to your macro arguments. This protects against start
not being an lvalue, and against end
having a side effect that gets applied or being a function that gets called every iteration.
But if you're trying to encapsulate a loop that gets called frequently, put it in its own separate function. It's safer and easier to understand and maintain.
You're probably looking for \
to continue a macro definition across several lines:
#define LOOP(start, end) \
for (int i = (start); i < (end); i++) { \
printf("%d\n", i); \
}
Since C doesn't require statements to be on separate lines, you can simply smush together into one long line:
#define M while (...) { ...; ...; }
Or you could escape newlines in the macro definition:
#define M \
while (...) { \
...; \
...; \
}
#define something for(;;) printf("hooray, i'm in infinite loop!");
int main() { something }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With