A
Alex
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland
Platform - Win32 (XP)
Quite by accident I stumbled across some wierd loop behavior. With the
pasted code I receive the output that follows.
I realize that the code is broken, because the inner loop fails to reset
j for each iteration of the outer loop (the fix is commented out). I
also know that this is better implemented by for loops.
However, shouldn't the inner loop stop at the 4th iteration anyway? Or
will the last iteration of the outer loop attempt to execute the
statement inside of the inner loop, because the inner loop's conditional
evaluation is at the bottom?
CODE ----------------------------------------------------------
/* 07L06.c nested do and while loops test */
#include <stdio.h>
int main()
{
int i, j;
i = 1;
j = 1;
while ( i <= 3 )
{
printf( "The start of iteration %d of the outer loop.\n", i );
//j = 1; //reset j should execute. I cut it out to
//duplicate the errant behavior
do
{
printf( " Iteration %d of the inner loop.\n", j );
j++;
}
while ( j < 4 );
printf( "The end of iteration %d of the outer loop.\n\n", i );
i++;
}
return 0;
}
OUTPUT ----------------------------------------------------------
The start of iteration 1 of the outer loop.
Iteration 1 of the inner loop.
Iteration 2 of the inner loop.
Iteration 3 of the inner loop.
The end of iteration 1 of the outer loop.
The start of iteration 2 of the outer loop.
Iteration 4 of the inner loop.
The end of iteration 2 of the outer loop.
The start of iteration 3 of the outer loop.
Iteration 5 of the inner loop.
The end of iteration 3 of the outer loop.
Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland
Platform - Win32 (XP)
Quite by accident I stumbled across some wierd loop behavior. With the
pasted code I receive the output that follows.
I realize that the code is broken, because the inner loop fails to reset
j for each iteration of the outer loop (the fix is commented out). I
also know that this is better implemented by for loops.
However, shouldn't the inner loop stop at the 4th iteration anyway? Or
will the last iteration of the outer loop attempt to execute the
statement inside of the inner loop, because the inner loop's conditional
evaluation is at the bottom?
CODE ----------------------------------------------------------
/* 07L06.c nested do and while loops test */
#include <stdio.h>
int main()
{
int i, j;
i = 1;
j = 1;
while ( i <= 3 )
{
printf( "The start of iteration %d of the outer loop.\n", i );
//j = 1; //reset j should execute. I cut it out to
//duplicate the errant behavior
do
{
printf( " Iteration %d of the inner loop.\n", j );
j++;
}
while ( j < 4 );
printf( "The end of iteration %d of the outer loop.\n\n", i );
i++;
}
return 0;
}
OUTPUT ----------------------------------------------------------
The start of iteration 1 of the outer loop.
Iteration 1 of the inner loop.
Iteration 2 of the inner loop.
Iteration 3 of the inner loop.
The end of iteration 1 of the outer loop.
The start of iteration 2 of the outer loop.
Iteration 4 of the inner loop.
The end of iteration 2 of the outer loop.
The start of iteration 3 of the outer loop.
Iteration 5 of the inner loop.
The end of iteration 3 of the outer loop.