473,378 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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?

Jan 7 '07 #1
6 2437
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 7 '07 #2
"vlsidesign" <fo*****@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
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.
Jan 7 '07 #3
On 2007-01-07, vlsidesign <fo*****@gmail.comwrote:
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

--
André Majorel <URL:http://www.teaser.fr/~amajorel/>
(Counterfeit: af****@worst.com ru*@revertive.com)
Religion: a magic device for turning unanswerable questions into
unquestionable answers. -- Art Gecko
Jan 7 '07 #4
Andre Majorel wrote:
vlsidesign wrote:
<snip>
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
Thanks for all the replies, they were very good .. very cool.

Jan 8 '07 #5
vlsidesign wrote:
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/...nt.990118.html
for one attempt at that elaboration.
--
Steve Summit
sc*@eskimo.com
Jan 8 '07 #6
Steve Summit wrote:
vlsidesign wrote:
><snip>
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 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 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/...nt.990118.html
for one attempt at that elaboration.
I'll check this out too, thanks Steve.

Jan 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
23
by: Mark Anderson | last post by:
A 'for' loop takes 3 arguments (initialize; test; increment). The 'test' must equate as true or false This doesn't work... x = 5; for (y=1; (y==5); y+=1) { alert(x * y); } ...nor does... x...
32
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...
7
by: Saturday7 | last post by:
You have a struct: struct a { char *p; }; struct a *apple; Which does this preincrement: apple or p?
22
by: Jan Richter | last post by:
Hi there, the Code below shows DJBs own implementation of strlen (str_len): unsigned int str_len(char *s) { register char *t; t = s; for (;;) { if (!*t) return t - s; ++t;
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
7
by: gmou | last post by:
Dear group, I am building a translator from C++ into VB (and into C#). At the moment, I have a hard time figuring out the equivalent of a 'for' loop in VB. Given C++ code: for( int i=0;...
7
by: Huck Phin | last post by:
OK, so I have looked and looked for something similar to my problem, and I cannot find one. I am writing a program for a HugeInteger, and I am attempting to overload the preincrement,...
6
by: Nobody | last post by:
hi there, suppose I want to iterate for a specific variable e.g. i , but for non regular (or consecutive) values. For example i=0,1,2,4,5,7,8 etc how can I do that with a for loop? MY...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.