# simple question about variables and loops

• 01-26-2005
InvariantLoop
simple question about variables and loops
Code:

```{         int i, j;         i = 1;         /* j = 1;  */                         while (i <=3){                         printf("The start of iteration %d of the outer loop.\n",i);                         j = 1; /* why this should be here and not under i=1;*/                 do                 {                 printf("        Ieteration %d of the inner loop,\n", j, i);                 j++;                 }         while (j<=4);         i++;         printf("The end of iteration %d of the outer loop.\n", i);         }         return 0; }```
i have commented out where my question is, someone explain to me why j=1; should be put right before the do-while loop.
• 01-26-2005
Exile
If you put the j=1 outside of the outer loop then it will never be reset. Each pass of the inner loop will start with j = 4, so the inner loopo will only go through once.

Immeditely before every while loop you have to set the starting point of your loop. Since teh inner loop goes through multiple times, each time through it you have to reset the counter's starting point.
• 01-26-2005
InvariantLoop
ah that makes sense