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.