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) { ... } 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
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
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.
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>
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.
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
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;}}}
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. 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>
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/
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.
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
...
|
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...
|
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...
|
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;...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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=()=>{
|
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...
|
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...
|
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...
|
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 :...
|
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...
|
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...
|
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...
|
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...
| |