472,983 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

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

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
2
by: Alex | last post by:
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...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.