C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Changing the variable of a for loop

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Arne Schmitz
Guest





PostPosted: Wed Dec 14, 2005 3:44 pm    Post subject: Changing the variable of a for loop Reply with quote



Does the changing of an iterator variable in a for loop result in undefined
behaviour? For example

for (int i = 0; i < 5; ++i)
i = 6;

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jeffrey Schwab
Guest





PostPosted: Wed Dec 14, 2005 7:26 pm    Post subject: Re: Changing the variable of a for loop Reply with quote



Arne Schmitz wrote:
Quote:
Does the changing of an iterator variable in a for loop result in undefined
behaviour? For example

for (int i = 0; i < 5; ++i)
i = 6;

No, but you probably should comment that.

i = 6; // Loop will end on next test.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
vord.fok@gmail.com
Guest





PostPosted: Wed Dec 14, 2005 7:27 pm    Post subject: Re: Changing the variable of a for loop Reply with quote



Undefined behaviour .. mm lets see .
You type in i=6 in the instruction .
the cycle is over and the he reads his for again . the statement says i
< 5 . the answer is i is bigger then 5 and he exit the loop . no
problem .
Why should that give any problems ?
your using this in coalition with an if statement or else there would
be no reason to make that loop in the first place .
that is a good solution if you want to go trough the loop without using
a break .

Greetings


[ 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





PostPosted: Wed Dec 14, 2005 7:27 pm    Post subject: Re: Changing the variable of a for loop Reply with quote


Arne Schmitz wrote:
Quote:
Does the changing of an iterator variable in a for loop result in undefined
behaviour? For example

for (int i = 0; i < 5; ++i)
i = 6;

No, you are free to change that variable in the loop, and the behavior
is well defined. What you've written is equivalent to (leaving aside
variable scope issues):

{
int i = 0;
while (i < 5) {
i = 6;
++i;
}
}

Best regards,

Tom


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.