473,473 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2654
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: simon place | last post by:
while playing about with inheriting from list to be able to cache macro properties i noticed this, the rate of summing a list seems to be over linear? its nearly 3 times faster to sum the sums of...
0
by: John Hunter | last post by:
I have a class that uses some extension code I have written and I am trying to track down some memory leaks, which I presume to be in my extension code. The class is question is in a python...
3
by: curi42 | last post by:
I am running two functions in a row that do the same thing. One runs in .14 seconds, the other 56. I'm confused. I wrote another version of the program and couldn't get the slow behavior again,...
4
by: hall | last post by:
Hi. I've come across someting strange. I was trying to make a for-loop execute repetadly until the function called inside it does not return true during the entire loop (see program below). ...
2
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0),...
11
by: Marlene Stebbins | last post by:
Something very strange is going on here. I don't know if it's a C problem or an implementation problem. The program reads data from a file and loads it into two arrays. When xy, x, y, *xlist and...
2
by: TB | last post by:
I am seeing a very strange problem as follows... I have a loop where a fair amount of processing is going on and near the top of the loop I access a class that has only static helper functions...
2
by: maxim khesin | last post by:
I was working on some code that compiled with vc6, eg.g for(int i;i<x;); if(i<j) {// etc which I rewrote to int i = 0; for(i = n;i<x;);
2
by: Jim Bancroft | last post by:
Hi everyone, I have a DropDownList I populate as outlined below. This is from my code-behind file: private void Page_Load(object sender, System.EventArgs e) { BindMyData(); DataBind(); }
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.