Strange do loop behavior

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.
 
M

Martin Ambuhl

Alex said:
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)

Luckily, none of the above has anything to do with your problem. If
they did, then you post would have almost certainly been off-topic in
comp.lang.c.
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?

You don't test the condition in
do {
printf( " Iteration %d of the inner loop.\n", j );
j++;
} while ( j < 4 );

until after the block has been executed. When the do ... while statement
is encountered, its body will be executed at least once; it you want to
check the condition at the beginning, do so using a while statement or a
for statement.

This 'always once' behavior is why the normal practice of wrapping
macros in 'do ... while(0);' works.
 
A

Alex

Martin said:
Luckily, none of the above has anything to do with your problem. If
they did, then you post would have almost certainly been off-topic in
comp.lang.c.



You don't test the condition in
do {
printf( " Iteration %d of the inner loop.\n", j );
j++;
} while ( j < 4 );

until after the block has been executed. When the do ... while statement
is encountered, its body will be executed at least once; it you want to
check the condition at the beginning, do so using a while statement or a
for statement.

This 'always once' behavior is why the normal practice of wrapping
macros in 'do ... while(0);' works.

OK, thanks for the confirmation :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top