Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for or while loop inside #define directive

How can I write a for/while loop inside a #define directive in C?

like image 216
hari Avatar asked Apr 22 '11 20:04

hari


People also ask

Can I use for loop inside while loop?

We can put a for loop inside a while loop or a do-while loop inside a for loop.

How does a for loop inside a while loop work?

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.

Can you have a for loop inside a while loop Python?

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.

Can we use for loop inside while loop in C?

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.


4 Answers

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.

like image 120
John Bode Avatar answered Oct 19 '22 22:10

John Bode


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);                    \
  }
like image 25
JaredPar Avatar answered Oct 19 '22 22:10

JaredPar


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 (...) { \
    ...; \
    ...; \
  }
like image 5
Thomas Edleson Avatar answered Oct 19 '22 23:10

Thomas Edleson


#define something for(;;) printf("hooray, i'm in infinite loop!");


int main() { something }
like image 4
nothrow Avatar answered Oct 19 '22 23:10

nothrow