why i++ instead of ++i in for loops

N

newtothis

I have been reading various texts in C/ C++ and Java. The for lops all
run along the lines of :



int i ;

for(i = 0 ; i < 4 ; i++)

{

.....

}



I understand the difference between ++i and i++, but I can not see why
i++ is used in these loops when, as I understand it, the steping
expression would be more alined to i = i + 1 in this type of case. The
logic of i++ is not the same. The final result might but the evaluation
process is not.



Ok I realise at the end of the loop the value of i is the same which
ever method you use. But that does not explaine why the preference.



Also from what I can see at the assembly code level



int i ;

for(i = 0 ; i < 4 ; ++i)

{

.....

}



and



int i ;

for(i = 0 ; i < 4 ; i++)

{

.....

}



are exactly the same.



Could some please explain the need to use i++ over ++i.
 
P

Peter van Merkerk

* snip *
Could some please explain the need to use i++ over ++i.

Strictly speaking there is no real need for i++, since "int x = i;
++i;" yields the same result as "int x = i++;". As you have noticed in
for loops with int as loop variable using ++i or i++ doesn't make a
difference with most compilers. Note that with iterators the ++i version
may very well be more efficient than i++.
 
A

Artie Gold

newtothis said:
I have been reading various texts in C/ C++ and Java. The for lops all
run along the lines of :

int i ;

for(i = 0 ; i < 4 ; i++)

{
....
}

I understand the difference between ++i and i++, but I can not see why
i++ is used in these loops when, as I understand it, the steping
expression would be more alined to i = i + 1 in this type of case. The
logic of i++ is not the same. The final result might but the evaluation
process is not.

Good call, "newtothis"!
Ok I realise at the end of the loop the value of i is the same which
ever method you use. But that does not explaine why the preference.

Also from what I can see at the assembly code level

int i ;

for(i = 0 ; i < 4 ; ++i)
{
....
}

and

int i ;

for(i = 0 ; i < 4 ; i++)
{
....
}

are exactly the same.
Which, by itself is irrelevant -- and only because a compiler can see
that the return value of `i++', i.e. the original value of `i' is
thrown
away.
Could some please explain the need to use i++ over ++i.
Sorry. Can't be done. ;-) There is no `need'.
What there *is* is `force of habit'. Historical artifact. Custom. Idiom.

While it is true that there is likely to be no difference in the
generated code when dealing with basic types, there often *is* a
difference -- and a potentially significant one -- when dealing with
user defined types (where `++', prefix and postfix are overloaded
operators).

Again, good call. Using the prefix form in such loops is to be
preferred -- it's just a hard habit to break!

HTH,
--ag
 
A

Andrew Koenig

I have been reading various texts in C/ C++ and Java. The for lops all
run along the lines of :
for(i = 0 ; i < 4 ; i++)



I understand the difference between ++i and i++, but I can not see why
i++ is used in these loops when, as I understand it, the steping
expression would be more alined to i = i + 1 in this type of case. The
logic of i++ is not the same. The final result might but the evaluation
process is not.
Ok I realise at the end of the loop the value of i is the same which
ever method you use. But that does not explaine why the preference.

You have hit on one of the reasons that "Accelerated C++" always uses the
"++i" form unless something is going to be done with the value of the
expression. So, for example, we would write

++i;

instead of

i++;

but we would write

n = a[i++];

if it were appropriate to do so--the point being that the value of "i++" is
used in the second example.

Incidentally, instead of

for (i = 0; i < 4; i++)

we would write

for (i = 0; i != 4; ++i)

because that way, we can use the same general form of loop for all kinds of
iterators in addition to integers.
 
J

jeffc

newtothis said:
I understand the difference between ++i and i++, but I can not see why
i++ is used in these loops when, as I understand it, the steping
expression would be more alined to i = i + 1 in this type of case. The
logic of i++ is not the same. The final result might but the evaluation
process is not.
Ok I realise at the end of the loop the value of i is the same which
ever method you use. But that does not explaine why the preference.

I think you've answered your own question. In that case, the result is the
same so it really doesn't matter. If it doesn't matter, i++ seems more
aesthetically pleasing to most people, since logically you see the variable
first, and then you see what to do with it. If these were classes with
member functions, it would look like this.

Int i(1);
i.increment();
vs.
increment().i;

The first looks more natural.
 
H

Howard

...So, for example, we would write

++i;

instead of

i++;

but we would write

n = a[i++];

if it were appropriate to do so--the point being that the value of "i++" is
used in the second example.

What do you mean that "the value of "i++" is used in the second example"?
If you're referring to n = a[i++], then the value that is used to index into
a is i, not i++. The increment is done afterwards. If, instead, you're
just saying that you should only use i++ when you need the value of i++,
again, I don't see how you can get that value, except in a following
statement, as in

x = a[i++];
n = a; // here we use the incremented value of i

I think I see what you were getting at, but your phrasing is a little
confusing.

-Howard
 
A

Andrew Koenig

...So, for example, we would write
++i;

instead of

i++;

but we would write

n = a[i++];

if it were appropriate to do so--the point being that the value of "i++"
is used in the second example.

What do you mean that "the value of "i++" is used in the second example"?
If you're referring to n = a[i++], then the value that is used to index into
a is i, not i++.

I meant exactly what I said: The value of i++ is used in the second
example. Now, as it happens, if i is an integer, then the value of i++ is a
copy of what the value of i was before i was incremented. If i is a
user-defined type, the value of i++ might be something different. Either
way, evaluating the expression a[i++] involves using the value of i++.
 
H

Howard

Rolf Magnus said:
Howard said:
...So, for example, we would write

++i;

instead of

i++;

but we would write

n = a[i++];

if it were appropriate to do so--the point being that the value of
"i++" is
used in the second example.

What do you mean that "the value of "i++" is used in the second
example"? If you're referring to n = a[i++], then the value that is
used to index into a is i, not i++.

i and i++ have the same value. The value of postfix++ is the value that
the operand had before the increment.
The increment is done afterwards.
Right.

If, instead, you're just saying that you should only use i++ when you
need the value of i++, again, I don't see how you can get that value,

int a = i++;

now a has the value of i++.
except in a following statement, as in

x = a[i++];
n = a; // here we use the incremented value of i

I think I see what you were getting at, but your phrasing is a little
confusing.


You seem to be confused by the term "value of i++". The value of i++ is
what you get when you e.g. assign "i++" to something, not the value of
i after i++ has been executed.


That's exactly where I was confused. Which is why I asked what he meant by
"the value of i++".

It seems very strange to refer to the "value of i++" unless you were
refering to the value you get by performing the ++ operation upon the i
variable, which is obviously not the intent. To me, it would be like
referring to the "value of 7+1", and intending to refer to the value 7
(before adding one), whereas in normal conversation, you'd be talking about
the value 8, which is what 7+1 is equal to. See why it's confusing (at
least to me)?

No argument here on the "truth" of what Andrew said...just confusion on the
terminology.

-Howard
 
N

newtothis

So from what has been said, except for may be the situation of an
oveloaded operator, the only reason for ++i vs i++ in the for loop is
programmer preference.

This then is close to the arguments of underscore vs mixedcase
vqariables.

Personal preference.



Why then dont the writeres of texts explain this instead of trying to
lay down some form of undefinable law?



Thanks for these comments as it clears up some programming "rules"
people have tried to push on me.
 
D

Duane Hebert

I think you've answered your own question. In that case, the result is the
same so it really doesn't matter. If it doesn't matter, i++ seems more
aesthetically pleasing to most people, since logically you see the variable
first, and then you see what to do with it. If these were classes with
member functions, it would look like this.

Actually that's not true. i++ has to create a temporary and ++i doesn't.
This may
not be a big deal when i is an int, but it gets pretty inefficient when i is
a larger type or
an iterator etc.
 
A

Andrew Koenig

So from what has been said, except for may be the situation of an
oveloaded operator, the only reason for ++i vs i++ in the for loop is
programmer preference.

Not quite. There's no difference in practice if i is an integer variable,
but if it's an iterator, there may be a performance difference. There is
also the philosophical question of whether it's a good thing to ask the
machine to do work that you know is going to be discarded.
Why then dont the writeres of texts explain this instead of trying to
lay down some form of undefinable law?

Some of them do.
 
P

Peter Koch Larsen

newtothis said:
So from what has been said, except for may be the situation of an
oveloaded operator, the only reason for ++i vs i++ in the for loop is
programmer preference.
Yes.


This then is close to the arguments of underscore vs mixedcase
vqariables.

Not at all. In situations as the one you mentioned, you usually
acquire a habit of either writing i++ or ++i. If you write ++i you are
assured of optimal performance, if you write i++, you're not. Thus you
should always prefer the ++i version.
Personal preference.



Why then dont the writeres of texts explain this instead of trying to
lay down some form of undefinable law?
I do believe that most good textbooks will tell you that you should
prefer ++i. At least they ought to do so.
Thanks for these comments as it clears up some programming "rules"
people have tried to push on me.

Kind regards
Peter Koch Larsen
 
R

Rolf Magnus

newtothis said:
I have been reading various texts in C/ C++ and Java. The for lops all
run along the lines of :



int i ;

for(i = 0 ; i < 4 ; i++)

{

....

}



I understand the difference between ++i and i++, but I can not see why
i++ is used in these loops when, as I understand it, the steping
expression would be more alined to i = i + 1 in this type of case. The
logic of i++ is not the same. The final result might but the
evaluation process is not.

For builtin types, it really doesn't matter. But in C++, you can write
and operator++ for your own class. And then it might matter, becaure
postfix ++ has to create a copy of the object so that the old value can
be returned. If you don't need the return value, that copy is
unnecessary. If the compiler doesn't do named return value
optimization, that copy might even need to be copied again, and all
that just to throw the result away. The postfix operator++ for an own
class might look something like this:

MyClass MyClass::eek:perator++(int)
{
MyClass retval(*this); // copy the object
// do whatever is needed to "increment" the object
reutrn retval; // return the copy by value
}

while prefix ++ might look like:

MyClass& MyClass::eek:perator++()
{
// do whatever is needed to "increment" the object
return *this; // return a refernce to the object
}

Therefore, it's considered a good habit to always use prefix ++ if the
return value is not needed.
 
N

newtothis

Thanks for all the comments. I did not intend to cause a general stir.
This though has confirmed that my use of ++i, when appropiate, in such
loops has been correct.



I realise at times the use of i++ is appropiate and is dependent on the
intent of the design and code.



I now just have to get over the issues of mixed case or underscores in
variables. That seems to have been discussed at some length anyway.



Thanks again



Newtothis
 
R

Rolf Magnus

Howard said:
...So, for example, we would write

++i;

instead of

i++;

but we would write

n = a[i++];

if it were appropriate to do so--the point being that the value of
"i++" is
used in the second example.

What do you mean that "the value of "i++" is used in the second
example"? If you're referring to n = a[i++], then the value that is
used to index into a is i, not i++.

i and i++ have the same value. The value of postfix++ is the value that
the operand had before the increment.
The increment is done afterwards.
Right.

If, instead, you're just saying that you should only use i++ when you
need the value of i++, again, I don't see how you can get that value,

int a = i++;

now a has the value of i++.
except in a following statement, as in

x = a[i++];
n = a; // here we use the incremented value of i

I think I see what you were getting at, but your phrasing is a little
confusing.


You seem to be confused by the term "value of i++". The value of i++ is
what you get when you e.g. assign "i++" to something, not the value of
i after i++ has been executed.
 
R

Rolf Magnus

Howard said:
Rolf Magnus said:
Howard said:
[email protected]...

...So, for example, we would write

++i;

instead of

i++;

but we would write

n = a[i++];

if it were appropriate to do so--the point being that the value of
"i++"
is
used in the second example.


What do you mean that "the value of "i++" is used in the second
example"? If you're referring to n = a[i++], then the value that is
used to index into a is i, not i++.

i and i++ have the same value. The value of postfix++ is the value
that the operand had before the increment.
The increment is done afterwards.
Right.

If, instead, you're just saying that you should only use i++ when
you need the value of i++, again, I don't see how you can get that
value,

int a = i++;

now a has the value of i++.
except in a following statement, as in

x = a[i++];
n = a; // here we use the incremented value of i

I think I see what you were getting at, but your phrasing is a
little confusing.


You seem to be confused by the term "value of i++". The value of i++
is what you get when you e.g. assign "i++" to something, not the
value of i after i++ has been executed.


That's exactly where I was confused. Which is why I asked what he
meant by "the value of i++".

It seems very strange to refer to the "value of i++" unless you were
refering to the value you get by performing the ++ operation upon the
i
variable, which is obviously not the intent. To me, it would be like
referring to the "value of 7+1", and intending to refer to the value 7
(before adding one), whereas in normal conversation, you'd be talking
about
the value 8, which is what 7+1 is equal to. See why it's confusing
(at least to me)?


Seems logical to me:

int x = 3 + 1;

the value of 3 + 1 is 4, so x is set to 4.

int y = x;

the value of x is 4, so y is set to 4.

int f(int i)
{
return i+1;
}

y = f(x);

the value of f(x) is 5, so y is set to 5 now.

int g(int& i)
{
int retval = i;
++i;
return retval;
}

y = g(x);

the value of g(x) is 4, so y is set to 4.

z = x++;

the value of x++ is 5, so z is set to 5 now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top