468,107 Members | 1,470 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

Strange do loop behavior

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.
Nov 15 '05 #1
2 2365
Alex wrote:
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.
Nov 15 '05 #2
Martin Ambuhl wrote:
Alex wrote:
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.


OK, thanks for the confirmation :)
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by simon place | last post: by
reply views Thread by John Hunter | last post: by
3 posts views Thread by curi42 | last post: by
4 posts views Thread by hall | last post: by
11 posts views Thread by Marlene Stebbins | last post: by
2 posts views Thread by maxim khesin | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.