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. 2 2671
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.
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 :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 smaller lists?
from time import clock
l=range(0,1000000)
|
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 module, not
extension code. I notice some strange behavior; perhaps a guru can
give me a pointer about what this may mean.
If I define the __del__...
|
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, only the fast.
I'm not sure what is causing it. Can anyone figure it out?
Here is my code (sorry it's a bit of a mess, but my cleaned up version...
|
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).
The two lines that confuse me are marked as (1) and (2).
count=0;
bool s(true);
|
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), an iterator that I've set in a
loop gets changed. Here's an example of the problem (sorry about the
lack of indentation, posting this from Google):
...
| |
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
*ylist are ints or floats there is no apparent problem. If these
variables are doubles, the program crashes. Furthermore, the
crashes occur only when...
|
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 to perform some calculations. After some number of
iterations, randomly, I'll get an uncaught NullValueException error on
one of these calls, as if...
|
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;);
|
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();
}
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |