 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Thomas Mang Guest
|
Posted: Thu Aug 28, 2003 10:39 am Post subject: Re: typo in a for loop has a strange affect |
|
|
whocares schrieb:
| Quote: | Hi group
I found the following typo in my code
for(int i=0; i++; i
{
/* do somthing*/
}
re-reading it I would of thought it would execute the loop once then i would
equal 1 and it would exit. but it actually loops for ever. Can anyone
explain the logic of this to me.
cheers
G
|
What implementation are you using?
Actually, the body of your loop should not be executed at all.
The conditional expression terminating the loop is
i++;
As i is initialized to zero, the result of i++ is zero too, which will be
implicitely converted to false - so the loop should terminate immediately.
regards,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tonci Tomic Guest
|
Posted: Thu Aug 28, 2003 10:40 am Post subject: Re: typo in a for loop has a strange affect |
|
|
"whocares" <gvingoe (AT) hotmail (DOT) com> wrote
| Quote: | I found the following typo in my code
for(int i=0; i++; i
{
/* do somthing*/
}
re-reading it I would of thought it would execute the loop once then i
would
equal 1 and it would exit. but it actually loops for ever. Can anyone
explain the logic of this to me.
|
Actually, it doesn't loop at all. i is set to 0, then it is incremented, but
i++ returns 0 and that's all.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Thu Aug 28, 2003 10:43 am Post subject: Re: typo in a for loop has a strange affect |
|
|
On 27 Aug 2003 11:33:34 -0400, "whocares" <gvingoe (AT) hotmail (DOT) com> wrote:
| Quote: | Hi group
I found the following typo in my code
for(int i=0; i++; i
{
/* do somthing*/
}
re-reading it I would of thought it would execute the loop once then i would
equal 1 and it would exit. but it actually loops for ever.
|
In that case you have a broken compiler; it should loop zero times.
| Quote: | Can anyone explain the logic of this to me.
|
Disregarding effect of 'continue', if any, the loop is equivalent to
{
int i = 0;
while( i++ )
{
// do something
i < x;
}
}
which again is equivalent to
{
int i = 0;
while( true )
{
int temp = i;
i = i + 1;
if( temp == 0 ) break;
// do something
i < x;
}
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stefan Heinzmann Guest
|
Posted: Thu Aug 28, 2003 10:44 am Post subject: Re: typo in a for loop has a strange affect |
|
|
| Quote: | for(int i=0; i++; i
{
/* do somthing*/
}
re-reading it I would of thought it would execute the loop once then i would
equal 1 and it would exit. but it actually loops for ever. Can anyone
explain the logic of this to me.
|
Strange, I would have expected the loop to terminate immediately. Does
your example contain another typo or are you using a weird compiler?
Cheers
Stefan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Falk Tannhäuser Guest
|
Posted: Thu Aug 28, 2003 10:46 am Post subject: Re: typo in a for loop has a strange affect |
|
|
| Quote: | for(int i=0; i++; i
{
/* do somthing*/
}
re-reading it I would of thought it would execute the loop once then i would
equal 1 and it would exit. but it actually loops for ever. Can anyone
explain the logic of this to me.
It should not be executed at all (and it isn't with gcc 3.2 which generates |
btw a 'statement with no effect' warning for the misplaced 'i
the value of the 'i++' will be 0.
Starting with another value for i, or writing ++i instead of i++ would
cause the loop to be executed at least until i reaches the platform dependent
INT_MAX. Then the result of incrementing an int holding the value INT_MAX
is implementation defined, but on the 2-complement implementations I'm aware
of it will wrap around to INT_MIN, so the loop will eventually terminate
when i reaches 0, rather than run for ever.
Falk Tannhäuser
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Thu Aug 28, 2003 10:47 am Post subject: Re: typo in a for loop has a strange affect |
|
|
whocares wrote:
| Quote: | I found the following typo in my code
for(int i=0; i++; i
{
/* do somthing*/
}
re-reading it I would of thought it would execute the loop once then i
would equal 1 and it would exit. but it actually loops for ever. Can
anyone explain the logic of this to me.
|
Are you sure you didn't make some error copying this ? Let me explain:
1. int i = 0
Creates the loop-counter. No surprises here.
2. i++
This postfix-increments i, which is at that time zero. The expression
converted to a boolean yields false. Since this is evaluated to decide if
the loop should be executed another time, the loop is not executed at all.
3. Nothing.
Since the condition for looping was false, the program continues after the
loop.
Note: if you changed the 'i++' to a '++i', things change. Then it loops
until you get an overflow[1] of the loop-counter.
Uli
[1] not sure if such behaviour is required by the standard.
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Thu Aug 28, 2003 2:31 pm Post subject: Re: typo in a for loop has a strange affect |
|
|
"whocares" wrote:
| Quote: | I found the following typo in my code
for(int i=0; i++; i
{
/* do somthing*/
}
re-reading it I would of thought it would execute
the loop once then i would equal 1 and it would
exit. but it actually loops for ever.
|
Really? Then your compiler is broken. On my system,
it executes the loop zero times (rather than once) and
then exits, which I believe is the correct behavior.
Are you sure you didn't accidentally type "++i"
instead of "i++" ? My compiler (GCC 3.3) also gave me
a warning, since the i
that context.
What compiler are you using?
Best regards,
Tom
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Guest
|
Posted: Thu Aug 28, 2003 2:50 pm Post subject: Re: typo in a for loop has a strange affect |
|
|
| Quote: | I found the following typo in my code
for(int i=0; i++; i
{
/* do somthing*/
}
re-reading it I would of thought it would execute the loop once then i would
equal 1 and it would exit. but it actually loops for ever. Can anyone
explain the logic of this to me.
|
Sure. The loop evaluates the condition ("i++") before each iteration,
and exits when it becomes false. s.6.5.3(1). Since the condition never
reaches 0 (== false) unless i is just about to overflow, the loop
lasts a long time.
-- Ron
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Aaron Bentley Guest
|
Posted: Thu Aug 28, 2003 2:54 pm Post subject: Re: typo in a for loop has a strange affect |
|
|
whocares wrote:
| Quote: | Hi group
I found the following typo in my code
for(int i=0; i++; i
{
/* do somthing*/
}
re-reading it I would of thought it would execute the loop once then i would
equal 1 and it would exit. but it actually loops for ever. Can anyone
explain the logic of this to me.
|
I think I know for loops pretty well, and I can't see how this would be
true. The loop can be rewritten like this:
int x;
{
int i=0;
while (i++)
{
cout<
i
}
}
Since you used i++ not ++i, the value 0 should be passed to the while(),
and since 0 is false, the body of the while loop should not be entered.
If you had used ++i, I could explain the behaviour. Then the value 1
(e.g. true) would have been passed to the while loop, and then the value
2, 3, 4, etc.
But even that won't loop forever. Eventually, i will reach INT_MAX, and
then ++i will produce INT_MIN, (a negative number). Finally, your
values will reach -2, -1 and then 0. At that point, the loop will exit.
You can see this effect more quickly if you make i a signed char.
HTH,
Aaron
--
Aaron Bentley
www.aaronbentley.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg P. Guest
|
Posted: Thu Aug 28, 2003 3:05 pm Post subject: Re: typo in a for loop has a strange affect |
|
|
"whocares" <gvingoe (AT) hotmail (DOT) com> wrote
| Quote: | I found the following typo in my code
for(int i=0; i++; i
{
/* do somthing*/
}
|
As you know, it should be: for(int i = 0; i < x; ++i){..}
Any *proper* compiler will not generate code to have the for statement loop
forever, or even at all. The structure of a for loop is:
for (initializations; boolean expressions; post operations)
In your case, the for loop is checking "i++" for a boolean result. However,
"i++" does return an integer (in your case). Your compiler is probably
guessing that since the return is a positive number, that the expression
results in "true" causing it to keep looping.
I tried your code with Digital Mars and it won't even execute the loop.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nils Petter Vaskinn Guest
|
Posted: Thu Aug 28, 2003 8:05 pm Post subject: Re: typo in a for loop has a strange affect |
|
|
On Wed, 27 Aug 2003 11:33:34 -0400, whocares wrote:
| Quote: | Hi group
I found the following typo in my code
for(int i=0; i++; i
{
/* do somthing*/
}
|
That's the same as:
int i = 0;
while (i++)
{
{
/* do something */
}
i
}
hth
NPV
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Corno Guest
|
Posted: Thu Aug 28, 2003 8:05 pm Post subject: Re: typo in a for loop has a strange affect |
|
|
"whocares" <gvingoe (AT) hotmail (DOT) com> wrote
| Quote: | Hi group
I found the following typo in my code
for(int i=0; i++; i
{
/* do somthing*/
}
re-reading it I would of thought it would execute the loop once then i
would
equal 1 and it would exit. but it actually loops for ever. Can anyone
explain the logic of this to me.
it will continue while the 'i++' returns true (thus any value except 0) and |
that will be forever (at least a mighty long time)
Corno
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Aug 28, 2003 8:33 pm Post subject: Re: typo in a for loop has a strange affect |
|
|
"whocares" <gvingoe (AT) hotmail (DOT) com> wrote
| Quote: | I found the following typo in my code
for(int i=0; i++; i
{
/* do somthing*/
}
re-reading it I would of thought it would execute the loop once then i
would equal 1 and it would exit. but it actually loops for ever. Can
anyone explain the logic of this to me.
|
Are you sure? I would expect it never to enter the loop, and this is
the behavior I get with the compilers I have access to (g++ and Sun CC).
After not entering the loop, of course, i is 1.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nils Petter Vaskinn Guest
|
Posted: Fri Aug 29, 2003 8:28 am Post subject: Re: typo in a for loop has a strange affect |
|
|
On Thu, 28 Aug 2003 11:05:52 -0400, Greg P. wrote:
| Quote: | "whocares" <gvingoe (AT) hotmail (DOT) com> wrote in message
news:biho3n$4b5$1 (AT) rdel (DOT) co.uk...
| I found the following typo in my code
|
| for(int i=0; i++; i
| {
| /* do somthing*/
| }
As you know, it should be: for(int i = 0; i < x; ++i){..}
Any *proper* compiler will not generate code to have the for statement loop
forever, or even at all. The structure of a for loop is:
|
Bzzzt. Wrong. But thanks for playing.
for (; {
// do something
}
Is completely legal. "do something" may or may not do something that cause
the program to exit or the loop to break;
| Quote: | I tried your code with Digital Mars and it won't even execute the loop.
|
Because the OP's loop shouldn't, he obviously made a typo when writing his
post or his program hung somewhere else.
regards
NPV
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Aug 29, 2003 8:29 am Post subject: Re: typo in a for loop has a strange affect |
|
|
In article <F1b3b.2786$_F1.424997 (AT) news20 (DOT) bellglobal.com>, Aaron Bentley
<aaron.bentley (AT) utoronto (DOT) ca> writes
| Quote: | But even that won't loop forever. Eventually, i will reach INT_MAX, and
then ++i will produce INT_MIN, (a negative number).
|
No, then you enter the domain of undefined behaviour. If we were using
an unsigned int all would be fine as adding one to UINT_MAX is required
to result in zero but there is no similar requirement for (signed) int.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|