473,808 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2455
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.goog legroups.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.co m ru*@revertive.c om)
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 "incrementi ng 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 "incrementi ng 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
2719
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
2215
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 = 5;
32
4663
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 where the loop will be traversed a fixed number of times. It is very flexible, and novice programmers should take care not to abuse the power it offers."
7
2427
by: Saturday7 | last post by:
You have a struct: struct a { char *p; }; struct a *apple; Which does this preincrement: apple or p?
22
2234
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
3153
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
7
2914
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; test_fct(i); increment_fct(i) ) { ... // body of the loop }
7
3121
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, postincrement, predecrement, and postdecrement operators. I realize that they are really all the same, and if I get one of the four operators working, I can get the rest. I have overloaded all of the other operators such as +, -, <, >, <=, etc. and there...
6
3034
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 solution which is not that elegant involves if statements (or switch statements) in the body of the loop: e.g.
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10631
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.