468,462 Members | 1,786 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,462 developers. It's quick & easy.

i++ or ++i ?

Please pardon a maybe very stupid question?
I've read and reread Bjarne Stroustrup on this, and I still don't "get it,"
so I wrote this to help me understand, and now I _really_ don't "get it."
Why, if one increments before, and the other after, do these snippets output
exactly the same?
#include <iostream>

int main() {
for(int i=0; i<10; i++)
std::cout<<i<<", ";
std::cout<<"\n";
for(int i=0;i<10;++i)
std::cout<<i<<", ";
std::cout<<"\n";
int b=7;
b++; std::cout<<b;
int c=7;
++c; std::cout<<"\n"<<c;
//????????? why? same outputs!
return 0;
}

_Same_output_.
What's the difference?
(I use these in a program, which doesn't crash....)
--
Peace
JB
jb@tetrahedraverse.com
Web: http://tetrahedraverse.com

Feb 8 '08 #1
10 25419
On Feb 8, 11:00*am, "John Brawley" <jgbraw...@charter.netwrote:
Please pardon a maybe very stupid question?
I've read and reread Bjarne Stroustrup on this, and I still don't "get it,"
so I wrote this to help me understand, and now I _really_ don't "get it."
Why, if one increments before, and the other after, do these snippets output
exactly the same?

#include <iostream>

int main() {
for(int i=0; i<10; i++)
std::cout<<i<<", ";
std::cout<<"\n";
for(int i=0;i<10;++i)
std::cout<<i<<", ";
std::cout<<"\n";
int b=7;
b++; std::cout<<b;
int c=7;
++c; std::cout<<"\n"<<c;
//????????? *why? *same outputs!
return 0;

}

_Same_output_.
What's the difference?
(I use these in a program, which doesn't crash....)
Try this and see:

int b=7;
std::cout << b++ <<endl;
int c=7;
std::cout << ++c << endl;
--
Fred Kleinschmidt
Feb 8 '08 #2
John Brawley schrieb:
Please pardon a maybe very stupid question?
I've read and reread Bjarne Stroustrup on this, and I still don't "get it,"
so I wrote this to help me understand, and now I _really_ don't "get it."
Why, if one increments before, and the other after, do these snippets output
exactly the same?
#include <iostream>

int main() {
for(int i=0; i<10; i++)
std::cout<<i<<", ";
std::cout<<"\n";
for(int i=0;i<10;++i)
std::cout<<i<<", ";
std::cout<<"\n";
int b=7;
b++; std::cout<<b;
int c=7;
++c; std::cout<<"\n"<<c;
//????????? why? same outputs!
return 0;
}

_Same_output_.
What's the difference?
(I use these in a program, which doesn't crash....)

Maybe it will help you do define the difference between ++i and i++;

The statements do the same with one difference:
The value of the statement "++i" is i+1
while the value of i++ is i:

#include <iostream>
using namespace std;

int main()
{
int i=6;
cout << "i++: " << i++ << endl; //Will return i = 6
int j=6;
cout << "++i: " << ++i << endl; //Will return i+1 = 7
return 0;
}

I hope this will help you!

Kind regards, Hans
Feb 8 '08 #3
John Brawley wrote:
Please pardon a maybe very stupid question?
I've read and reread Bjarne Stroustrup on this, and I still don't "get it,"
so I wrote this to help me understand, and now I _really_ don't "get it."
Why, if one increments before, and the other after, do these snippets output
exactly the same?
#include <iostream>

int main() {
for(int i=0; i<10; i++)
std::cout<<i<<", ";
std::cout<<"\n";
for(int i=0;i<10;++i)
std::cout<<i<<", ";
std::cout<<"\n";
int b=7;
b++; std::cout<<b;
int c=7;
++c; std::cout<<"\n"<<c;
//????????? why? same outputs!
return 0;
}

_Same_output_.
What's the difference?
(I use these in a program, which doesn't crash....)

Along the answers of the others, my advice is, read a good introductory
C++ book cover to cover, before reading "The C++ Programming Language"
3rd Edition or Special Edition cover to cover.
Feb 8 '08 #4
Hans Mull <de*******@googlemail.comwrites:
[...]
You can directly print them via cout:
cout << ++i;
cout << i++;
Yes, but if you want to see non-puzzling results, you'd better use two
variables:

int i=0;cout<<"++i = "<<++i<<endl;
int j=0;cout<<"j++ = "<<j++<<endl;

--
__Pascal Bourguignon__ http://www.informatimago.com/
The mighty hunter
Returns with gifts of plump birds,
Your foot just squashed one.
Feb 8 '08 #5
On Feb 8, 2:59 pm, "John Brawley" <jgbraw...@charter.netwrote:
Andrey wrote:

the
result of '++i' expression is the "new" value of 'i' (the value "after"
the increment), while the result of 'i++' expression is the "old" value
of 'i' (the value "before" the increment).
(I'll thank y'all even one more when I finally understand this permanently.)
A common description of the differences between the two is

++i increments i before
i++ increments i after

Unfortunately that definition works only in C. There is a better
description that works in both languages:

++i increments i and uses i
i++ takes a copy of i, increments i, and uses the copy

Daniel T.'s recommendation of looking at the implementation is helpful
in seeing this. So, here is how it boils down:

foo(++i) is the same thing as

++i;
foo(i);

On the other hand, foo(i++) is the same thing as

const int compiler_generated_temp = i;
++i;
foo(compiler_generated_temp);

Ali
Feb 8 '08 #6
John Brawley wrote:
>
(Oh, one last: I do appreciate the recommendation to read a good book on C++
Actually: "A good *introductory* book on C++".

"The C++ Programming Language" 3rd Edition or Special Edition is an
excellent book itself, better suited to intermediate C++ programmers IMHO.
Feb 8 '08 #7

"Ioannis Vranos" <iv*****@nospam.no.spamfreemail.grwrote in message
news:fo**********@ulysses.noc.ntua.gr...
John Brawley wrote:

(Oh, one last: I do appreciate the recommendation to read a good book on
C++
>
Actually: "A good *introductory* book on C++".

"The C++ Programming Language" 3rd Edition or Special Edition is an
excellent book itself, better suited to intermediate C++ programmers IMHO.
I take your point, and would certainly agree, for any newbie who was trying
to *learn to program* in C++. However, my case was/is different: I was
trying (well, past tense; I've already succeeded) to *write a program* in
C++.
Those are actually, if you think about it, very different motivations
driving perhaps equally different paths and needs. Having never done so
before, one can pick up an oboe and in a while learn to play one piece (say,
"Stranger on the Shore" --Acker Bilk) on it rather indistinguishably from a
musician, but if asked to play a different piece, nothing but squawks might
emerge. (*g*)
I had *used* all this code to get something working. I didn't necessarily
_understand_ why or how it worked in every case (especially this ++i / i++
case), but no matter: the program does exactly what I wanted it to.
--
Peace
JB
jb@tetrahedraverse.com
Web: http://tetrahedraverse.com

Feb 9 '08 #8
John Brawley wrote:
"Ioannis Vranos" <iv*****@nospam.no.spamfreemail.grwrote in message
news:fo**********@ulysses.noc.ntua.gr...
>John Brawley wrote:
>>(Oh, one last: I do appreciate the recommendation to read a good book on
C++
>Actually: "A good *introductory* book on C++".

"The C++ Programming Language" 3rd Edition or Special Edition is an
excellent book itself, better suited to intermediate C++ programmers IMHO.

I take your point, and would certainly agree, for any newbie who was trying
to *learn to program* in C++. However, my case was/is different: I was
trying (well, past tense; I've already succeeded) to *write a program* in
C++.
Those are actually, if you think about it, very different motivations
driving perhaps equally different paths and needs. Having never done so
before, one can pick up an oboe and in a while learn to play one piece (say,
"Stranger on the Shore" --Acker Bilk) on it rather indistinguishably from a
musician, but if asked to play a different piece, nothing but squawks might
emerge. (*g*)
I had *used* all this code to get something working. I didn't necessarily
_understand_ why or how it worked in every case (especially this ++i / i++
case), but no matter: the program does exactly what I wanted it to.

OK it is up to you to decide what you need, but if you had read a good
introductory C++ book, I think you would have understood the difference
between i++ and ++i in the first place. :-)
Feb 9 '08 #9

"Ioannis Vranos" <iv*****@nospam.no.spamfreemail.grwrote in message
news:fo***********@ulysses.noc.ntua.gr...
John Brawley wrote:
"Ioannis Vranos" <iv*****@nospam.no.spamfreemail.grwrote in message
news:fo**********@ulysses.noc.ntua.gr...
John Brawley wrote:

(Oh, one last: I do appreciate the recommendation to read a good book
on
C++
Actually: "A good *introductory* book on C++".

"The C++ Programming Language" 3rd Edition or Special Edition is an
excellent book itself, better suited to intermediate C++ programmers
IMHO.

I take your point, and would certainly agree, for any newbie who was
trying
to *learn to program* in C++. However, my case was/is different: I was
trying (well, past tense; I've already succeeded) to *write a program*
in
C++.
Those are actually, if you think about it, very different motivations
driving perhaps equally different paths and needs. Having never done so
before, one can pick up an oboe and in a while learn to play one piece
(say,
"Stranger on the Shore" --Acker Bilk) on it rather indistinguishably
from a
musician, but if asked to play a different piece, nothing but squawks
might
emerge. (*g*)
I had *used* all this code to get something working. I didn't
necessarily
_understand_ why or how it worked in every case (especially this ++i /
i++
case), but no matter: the program does exactly what I wanted it to.


OK it is up to you to decide what you need, but if you had read a good
introductory C++ book, I think you would have understood the difference
between i++ and ++i in the first place. :-)
Doubtless I would have.
Thank you for the kind advice.
I often play on grounds for which I am not qualified.
So far, no serious injuries....
(*grin*)
--
Peace
JB
jb@tetrahedraverse.com
Web: http://tetrahedraverse.com

Feb 9 '08 #10
On Feb 9, 12:51 am, acehr...@gmail.com wrote:
On Feb 8, 2:59 pm, "John Brawley" <jgbraw...@charter.netwrote:
A common description of the differences between the two is
++i increments i before
i++ increments i after
Unfortunately that definition works only in C. There is a better
description that works in both languages:
++i increments i and uses i
i++ takes a copy of i, increments i, and uses the copy
And what's the difference between the two descriptions?

An expression has two characteristics: its side effects, and its
value. The side effects of the two expressions are the same.
The value is different. If you don't use the value, there's
(conceptually, at least) no value.

If you're writing a user defined ++, of course, and you want to
make it behave like the built in one (always a good idea), then
taking a copy before incrementing is usually the simplest way.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 9 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by William C. White | last post: by
2 posts views Thread by Albert Ahtenberg | last post: by
3 posts views Thread by James | last post: by
reply views Thread by Ollivier Robert | last post: by
1 post views Thread by Richard Galli | last post: by
4 posts views Thread by Albert Ahtenberg | last post: by
1 post views Thread by inderjit S Gabrie | last post: by
2 posts views Thread by Jack | last post: by
3 posts views Thread by Sandwick | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.