preincrement and postincrement var in a for loop only matter in body

V

vlsidesign

If I have a "for" loop like this:
for (i = 0; i < 10; i++)
or like this
for (i = 0; i < 10; ++i)
both of the "for" loops will function the same. I also noticed if I use
a variable called j within the "for" loop body, and put these statement
in the "for" loop body like this:
printf("j var is: %d\n", ++j);
printf("j var is: %d\n", j++);
In the first statement, "j" increments first, and then prints "j"
value, and the latter statement, "j" increments after printing "j"
value.

I was wondering how it works, is it that in some cases you use the
actual variable, and in other cases you use the 'return value' of the
actual operator?
 
R

Richard Heathfield

vlsidesign said:
If I have a "for" loop like this:
for (i = 0; i < 10; i++)
or like this
for (i = 0; i < 10; ++i)
both of the "for" loops will function the same. I also noticed if I use
a variable called j within the "for" loop body, and put these statement
in the "for" loop body like this:
printf("j var is: %d\n", ++j);
printf("j var is: %d\n", j++);
In the first statement, "j" increments first, and then prints "j"
value, and the latter statement, "j" increments after printing "j"
value.

I was wondering how it works, is it that in some cases you use the
actual variable, and in other cases you use the 'return value' of the
actual operator?

In your printf, you are using the value of the expression ++j, which can be
thought of as "one higher than the value j had at the previous sequence
point". As a side effect, the value of j is changed, but never mind that.

In your other printf, you are using the value of the expression j++, which
can be thought of as "the value j had at the previous sequence point". As a
side effect, the value of j is changed, but never mind that.

In your for loops, you aren't interested in the values of i++ or ++i, only
in the side effects of those operations, so you don't bother to store the
values of those expressions. If you did so, you would see the same
distinction as with your j++ and ++j.
 
D

David T. Ashley

vlsidesign said:
If I have a "for" loop like this:
for (i = 0; i < 10; i++)
or like this
for (i = 0; i < 10; ++i)
both of the "for" loops will function the same. I also noticed if I use
a variable called j within the "for" loop body, and put these statement
in the "for" loop body like this:
printf("j var is: %d\n", ++j);
printf("j var is: %d\n", j++);
In the first statement, "j" increments first, and then prints "j"
value, and the latter statement, "j" increments after printing "j"
value.

I was wondering how it works, is it that in some cases you use the
actual variable, and in other cases you use the 'return value' of the
actual operator?

RH provided a great description of how it works (values of expressions).

Just let me point out that you can "dumb down" your 'C' code to the level
you're comfortable with. There are some 'C' constructs that I just don't
feel comfortable with, so I might write something like:

while (*i)
{
do_this(*i);
i++;
}

even while I know there are terser ways to do it.

If this kind of thing:
printf("j var is: %d\n", ++j);

makes you nervous, you can just write:

j++;
printf("j var is: %d\n", j);

It is not required that you use every feature of the language. It may take
many months before you are comfy with them all.

But eventually, you HAVE TO get comfy with them all. For example, when you
see:

if (x && (*x > 10))

you do have to understand that "x" won't be dereferenced unless it is
non-NULL (which may be important), especially when reading code written by
others who may have different comfort levels with different features of the
language.
 
A

Andre Majorel

If I have a "for" loop like this:
for (i = 0; i < 10; i++)
or like this
for (i = 0; i < 10; ++i)
both of the "for" loops will function the same.
Right.

I also noticed if I use a variable called j within the "for"
loop body, and put these statement in the "for" loop body like
this:
printf("j var is: %d\n", ++j);
printf("j var is: %d\n", j++);
In the first statement, "j" increments first, and then prints "j"
value, and the latter statement, "j" increments after printing "j"
value.
Right.

I was wondering how it works, is it that in some cases you use the
actual variable, and in other cases you use the 'return value' of the
actual operator?

That's the general idea. Suppose you have a statement like "foo
(j++);" and register r0 happens to hold the value of j. The
compiler could output something like this :

push r0
inc r0
jsr foo

For "foo(++j)", it would be :

inc r0
push r0
jsr foo
 
S

Steve Summit

vlsidesign said:
If I have a "for" loop like this:
for (i = 0; i < 10; i++)
or like this
for (i = 0; i < 10; ++i)
both of the "for" loops will function the same.

Correct. See also the comp.lang.c FAQ list, question 3.12.
I also noticed if I use a variable called j within the "for" loop body,
and put these statement in the "for" loop body like this:
printf("j var is: %d\n", ++j);
printf("j var is: %d\n", j++);
In the first statement, "j" increments first, and then prints "j"
value, and the latter statement, "j" increments after printing "j"
value.

Also correct. This is exactly the difference between the two
uses, prefix and postfix, of the ++ operator. Notice that this
distinction applies *everywhere*, not just when you're inside the
body of a for loop.
I was wondering how it works, is it that in some cases you use the
actual variable, and in other cases you use the 'return value' of the
actual operator?

I'm not sure what you mean. What you get is not arbitrary --
in the case of ++j what you get as the "return value" is always,
exactly, the incremented value, and what you get from j++ is
always the old, unincremented value.

I suspect you may be overlooking another essential distinction:
that between "adding one" and "incrementing a variable".
(At first this is going to sound too basic to be worth discussing,
but bear with me.)

If I want to find out what one more than i is, I can just use the
"+" operator:

int i = 10;
printf("i is %d\n", i);
printf("one more than i is %d\n", i + 1);

Notice that in this case i's value did *not* change.

If I want to actually change i's value, make it one more than it
was before, I can use the standard idiom "i = i + 1":

int i = 10;
printf("i is %d\n", i);
i = i + 1;
printf("i is now %d\n", i);

The important point here is that the "+" operator only does
addition; it doesn't actually change the values of any variables.
If you want to actually change the value of a variable, one way
of doing this is with the "=" or assignment operator.

Now, what you need to understand is that the ++ operator does
*both* things. It does some addition, *and* it modifies the
value of the variable. After you do "i++", i will contain a
value one greater than it did before, just as if you'd said
"i = i + 1". After you do "++i", i will contain a value one
greater than it did before, also just as if you'd said "i = i + 1".

If ++ always adds one to i and changes i's value, what's the
difference between i++ and ++i? The only difference is the
"return value" of the expression.

When you say

for(i = 0; i < 10; i++)
or
for(i = 0; i < 10; ++i)

you're not using the "return value", so you can't see any
difference, and in fact there is no difference. But when you're
using ++ in a subexpression within a larger expression, for
example when you print its "return value" by passing it to printf:

printf("j var is: %d\n", ++j);
printf("j var is: %d\n", j++);

then there is a real, observable difference, as you've seen.

You may still be wondering what practical use there is for this
distinction between ++j and j++. That's an excellent question,
but doing it proper justice can get a bit elaborate. See
http://www.eskimo.com/~scs/readings/autoincrement.990118.html
for one attempt at that elaboration.
 
V

vlsidesign

Andre said:
vlsidesign wrote:

That's the general idea. Suppose you have a statement like "foo
(j++);" and register r0 happens to hold the value of j. The
compiler could output something like this :

push r0
inc r0
jsr foo

For "foo(++j)", it would be :

inc r0
push r0
jsr foo

Thanks for all the replies, they were very good .. very cool.
 
V

vlsidesign

Steve said:
Correct. See also the comp.lang.c FAQ list, question 3.12.
I found your website c-faq.com, I need to spend some time checking it
out. Thanks.
I'm not sure what you mean. What you get is not arbitrary --
in the case of ++j what you get as the "return value" is always,
exactly, the incremented value, and what you get from j++ is
always the old, unincremented value.
I didn't quite understand "how" to think about ++j and j++. But now I
have a better idea, in some contexts, I see it used as an "expression"
or the "return value". Other contexts, it is used as the value of 'j'
after the ++ operator is through doing it's stuff. Anyway, you guys
said it better, I just need to re-read the posts, and think about it
some more.
I suspect you may be overlooking another essential distinction:
that between "adding one" and "incrementing a variable".
(At first this is going to sound too basic to be worth discussing,
but bear with me.)
This whole section was also helpful, thanks.
You may still be wondering what practical use there is for this
distinction between ++j and j++. That's an excellent question,
but doing it proper justice can get a bit elaborate. See
http://www.eskimo.com/~scs/readings/autoincrement.990118.html
for one attempt at that elaboration.
I'll check this out too, thanks Steve.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top