Velocity Reviews > Diffrence between ++i and i++

# Diffrence between ++i and i++

John Bode
Guest
Posts: n/a

 04-13-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Luai) wrote in message news:<(E-Mail Removed). com>...
> I made my midterm exam in the C language course.
> I lost 18 marks off 100 because I didn't relaize this killing fact:
>
> in (for loops) there is no difference between incrementing the loop in
> these two ways:
>
> for (i=0; i < 10 ; ++i)
>
> and
>
> for (i=0; i < 10 ; i++)
>
> ( notice the difference is between i++ and ++i )
>

A proper understanding of the pre and post ++ and -- operators is
vital if you're going to be programming in C. I don't know if that
particular question should have been worth 18 points, though.

The expression i++ evaluates to the current value of i; as a *side
affect*, i is incremented some time before the next sequence point.

The expression ++i evaluates to the current value of i *plus 1*; as a
*side affect*, i is incremented sometime before the next sequence
point.

The -- operator works the same way, just replace "plus" with "minus"
and "increment" with "decrement".

In both cases above, you don't care about what the expression
evaluates to, just the side effect (incrementing i by 1), so either
expression works just as well. OTOH, if the autoincrement/decrement
expression is part of a larger expression, one or the other may be
called for. For example, given a variable i initialized to 1, the
expressions

j = i++ * 2 /* i == 1, j = 1 * 2 */

and

j = ++i * 2 /* i == 1, j = 2 * 2 */

will assign different results for j, so whether you use pre- or
post-increment does matter.

Christian Bau
Guest
Posts: n/a

 04-13-2004
In article <(E-Mail Removed)>,
Ben Pfaff <(E-Mail Removed)> wrote:

> Andrew Clark <(E-Mail Removed)> writes:
>
> > Ben Pfaff <(E-Mail Removed)> wrote in
> > news:(E-Mail Removed):
> >
> >> (E-Mail Removed) (Luai) writes:
> >>
> >>> in (for loops) there is no difference between incrementing the loop in
> >>> these two ways:
> >>>
> >>> for (i=0; i < 10 ; ++i)
> >>>
> >>> and
> >>>
> >>> for (i=0; i < 10 ; i++)
> >>
> >> Why and how did you think they might be different?

> >
> > I recall an exam where I was marked off for writing one of these (I
> > forget which one), and to correct it the instructor wrote the other one.

>
> You'll have to be more specific. When ++i or i++ is a full
> expression, they are equivalent. When one of them is a
> subexpression of a larger expression, they may not be
> equivalent. So if your instructor took off points in the former
> case, he (or she) was simply wrong, but in the latter case he may
> have been justified.

Maybe the exam question was:

"Write a full expression which increases i by one, without using a
postincrement operator"...

Mabden
Guest
Posts: n/a

 05-07-2004
"E. Robert Tisdale" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Luai wrote:
>
> > I made my midterm exam in the C language course.
> > I lost 18 marks off 100 because I didn't realize this killing fact:
> >
> > in (for loops) there is no difference
> > between incrementing the loop in these two ways:
> >
> > for (i = 0; i < 10; ++i)
> >
> > and
> >
> > for (i = 0; i < 10; i++)
> >
> > (notice the difference is between i++ and ++i)
> >

>
> There is no difference.
> C++ programmers prefer ++i only as a "good habit"
> because both the pre increment and post increment operator++
> may be overloaded for a class for very large objects
> where i++ returns a copy of the original object
> but ++i merely returns a reference
> after "incrementing" the original object.
> If you are going to write C++ programs as well as C programs,
> you probably should use ++i wherever you have a choice.
>

Didn't you mean "++C Programmers..."?

--
Mabden