468,484 Members | 2,096 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

A question on for loop

Would there be any difference in effect in these two versions of code? Just assume that all variables are initialized. I think a better question might be is there a case where the effects of two versions differ?

VERSION 1:
for(; i < R; i++) { ... }

VERSION 2:
for(; i < R; ++i) { ... }
Dec 17 '07 #1
12 1023
In article <87************@student.uts.edu.au>,
Logan Lee <Lo*********@student.uts.edu.auwrote:
>Would there be any difference in effect in these two versions of code? Just
assume that all variables are initialized. I think a better question might be
is there a case where the effects of two versions differ?

VERSION 1:
for(; i < R; i++) { ... }

VERSION 2:
for(; i < R; ++i) { ... }
No, they're the same.

-- Richard
--
:wq
Dec 17 '07 #2
Logan Lee <Lo*********@student.uts.edu.auwrites:
Would there be any difference in effect in these two versions of code?
Just assume that all variables are initialized. I think a better
question might be is there a case where the effects of two versions
differ?

VERSION 1: for(; i < R; i++) { ... }

VERSION 2: for(; i < R; ++i) { ... }
These loops are equivalent. Furthermore, with many (most?)
compilers, the generated code will be identical.
--
Ben Pfaff
http://benpfaff.org
Dec 17 '07 #3
On Tue, 18 Dec 2007 09:09:09 +1100, Logan Lee
<Lo*********@student.uts.edu.auwrote:
>Would there be any difference in effect in these two versions of code? Just assume that all variables are initialized. I think a better question might be is there a case where the effects of two versions differ?

VERSION 1:
for(; i < R; i++) { ... }

VERSION 2:
for(; i < R; ++i) { ... }
First, writing a for loop and then not initializing variable i in the
beginning is a detestable practice. The whole point of "for" is that
the 3 control elements are placed at the top of the loop so it is
clear exactly what is going on. If I have to go up from there to find
out what the initial value of i is supposed to be it makes the code
harder to read and maintain. I don't know where this practice arose
but it should be discouraged wherever it is found. If you are going to
depend on i being initialized somewhere else, you may as well have
written the loop as a while construct.

As for V1 vs. V2, they are the same.
Dec 17 '07 #4
Logan Lee wrote:
Would there be any difference in effect in these two versions of code? Just assume that all variables are initialized. I think a better question might be is there a case where the effects of two versions differ?

VERSION 1:
for(; i < R; i++) { ... }

VERSION 2:
for(; i < R; ++i) { ... }
Nope. This is also the same:

VERSION 3:
for(; i < R; i+=1) { ... }

Furthermore, the speed recommendation found in K&R for usage of
pre-increment, is quite outdated.

--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Dec 18 '07 #5
On Dec 17, 3:15 pm, Geoff <ge...@invalid.invalidwrote:
On Tue, 18 Dec 2007 09:09:09 +1100, Logan Lee

<Logan.W....@student.uts.edu.auwrote:
Would there be any difference in effect in these two versions of code? Just assume that all variables are initialized. I think a better question might be is there a case where the effects of two versions differ?
VERSION 1:
for(; i < R; i++) { ... }
VERSION 2:
for(; i < R; ++i) { ... }

First, writing a for loop and then not initializing variable i in the
beginning is a detestable practice.
It's OK if it was produced by some sort of grotesque computation that
you don't want to stick between the '(' and the ','.
P.S.
I prefer this:
for(;;){}
to:
while(1){}

Not that there is any reason for it (just got tired of lint screaming
at me about conditional is always true).
The whole point of "for" is that
the 3 control elements are placed at the top of the loop so it is
clear exactly what is going on. If I have to go up from there to find
out what the initial value of i is supposed to be it makes the code
harder to read and maintain. I don't know where this practice arose
but it should be discouraged wherever it is found. If you are going to
depend on i being initialized somewhere else, you may as well have
written the loop as a while construct.
There are exceptions to every rule. But in general I do agree with
your position.
As for V1 vs. V2, they are the same.
Dec 18 '07 #6
VERSION 1:
for(; i < R; i++)
VERSION 2:
for(; i < R; ++i)
as u have initialized i somewhere else n skipped it in for loop
the increment can also be skipped from for loop n be written as

for ( ; i < R; )
{
i++; or ++i;
}

so you see when incrementing a variable is the only performed
operation in a statement ++i & i++ are all but same. thats why both
the versions are same
Dec 18 '07 #7
user923005 <dc*****@connx.comwrites:
On Dec 17, 3:15 pm, Geoff <ge...@invalid.invalidwrote:
>On Tue, 18 Dec 2007 09:09:09 +1100, Logan Lee
<Logan.W....@student.uts.edu.auwrote:
>VERSION 1:
for(; i < R; i++) { ... }

First, writing a for loop and then not initializing variable i in the
beginning is a detestable practice.

It's OK if it was produced by some sort of grotesque computation that
you don't want to stick between the '(' and the ','.
Another possibility: fairly often I find myself writing code like
this:
for (i = 0; i < old_limit; i++)
...
for (; i < new_limit; i++)
...
Seems reasonable to me.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Dec 18 '07 #8
On Mon, 17 Dec 2007 23:18:44 -0800, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
>user923005 <dc*****@connx.comwrites:
>On Dec 17, 3:15 pm, Geoff <ge...@invalid.invalidwrote:
>>On Tue, 18 Dec 2007 09:09:09 +1100, Logan Lee
<Logan.W....@student.uts.edu.auwrote:
VERSION 1:
for(; i < R; i++) { ... }

First, writing a for loop and then not initializing variable i in the
beginning is a detestable practice.

It's OK if it was produced by some sort of grotesque computation that
you don't want to stick between the '(' and the ','.

Another possibility: fairly often I find myself writing code like
this:
for (i = 0; i < old_limit; i++)
...
for (; i < new_limit; i++)
...
Seems reasonable to me.
Yes, if it's reasonably proximate to the other construct I don't get
as irate about it, but I have seen a quite a few beginners picking it
up and usually initializing i at the declaration and carrying it
through about 30 or 40 lines and the writing for( ; i < something;i++)
and away we go. :) Simple init, simple idiom badly used.

Dec 18 '07 #9
sr**************@gmail.com wrote:
VERSION 1:
for(; i < R; i++)

VERSION 2:
for(; i < R; ++i)

as u have initialized i somewhere else n skipped it in for loop
the increment can also be skipped from for loop n be written as

for ( ; i < R; )
{
i++; or ++i;
}
In this case a while loop would be more suitable.

<snip>

Dec 18 '07 #10
Hello,

Geoff wrote:
On Mon, 17 Dec 2007 23:18:44 -0800, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
[...]
>>Another possibility: fairly often I find myself writing code like
this:
for (i = 0; i < old_limit; i++)
...
for (; i < new_limit; i++)
...
Seems reasonable to me.
[...]
Simple init, simple idiom badly used.
What about writing the second line as

for (i = i; i < new_limit; i++)
...

The idiom would be used consistently, the meaning is clear, and any
decent compiler should optimize the "i = i" away.

Regards,
Spiro.

--
Spiro R. Trikaliotis http://opencbm.sf.net/
http://www.trikaliotis.net/ http://www.viceteam.org/
Dec 18 '07 #11
Spiro Trikaliotis wrote:
Hello,

Geoff wrote:
>On Mon, 17 Dec 2007 23:18:44 -0800, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
[...]
>>>Another possibility: fairly often I find myself writing code like
this:
for (i = 0; i < old_limit; i++)
...
for (; i < new_limit; i++)
...
Seems reasonable to me.
[...]
>Simple init, simple idiom badly used.

What about writing the second line as

for (i = i; i < new_limit; i++)
...

The idiom would be used consistently, the meaning is clear, and any
decent compiler should optimize the "i = i" away.
The "idiom" is that the initialisation is done close to the other loop
control statements. Replacing the empty statement with a redundant one
would still be, IMHO, misuse of the "intent" of such a type of loop
construct.

Dec 18 '07 #12
Hello,

santosh wrote:
Spiro Trikaliotis wrote:
[...]
>What about writing the second line as

for (i = i; i < new_limit; i++)
...
[...]
The "idiom" is that the initialisation is done close to the other loop
control statements. Replacing the empty statement with a redundant one
would still be, IMHO, misuse of the "intent" of such a type of loop
construct.
I do not consider this construct a real good one, but at least more
consistent than the previous one. But you are right, looking at it after
some days, it really looks ugly, and it does not have any real
advantage.

And: No, I never used it this way.

Regards,
Spiro.

--
Spiro R. Trikaliotis http://opencbm.sf.net/
http://www.trikaliotis.net/ http://www.viceteam.org/
Dec 20 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Charles Alexander | last post: by
43 posts views Thread by Gremlin | last post: by
5 posts views Thread by Martin Schou | last post: by
32 posts views Thread by Toby Newman | last post: by
2 posts views Thread by Alex | last post: by
3 posts views Thread by Ben R. | last post: by
32 posts views Thread by cj | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.