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 10 25616
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
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
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.
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.
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
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.
"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
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. :-)
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: William C. White |
last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using
cURL? Our website is hosted on a shared drive and the webhost company
doesn't installed additional software (such as cURL)...
|
by: Albert Ahtenberg |
last post by:
Hello,
I don't know if it is only me but I was sure that header("Location:url")
redirects the browser instantly to URL, or at least stops the execution of
the code. But appearantely it continues...
|
by: James |
last post by:
Hi,
I have a form with 2 fields.
'A'
'B'
The user completes one of the fields and the form is submitted.
On the results page I want to run a query, but this will change
subject to which...
|
by: Ollivier Robert |
last post by:
Hello,
I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9
system. The link succeeds but everytime I try to run php, I get a SEGV from
inside the libcnltsh.so library.
...
|
by: Richard Galli |
last post by:
I want viewers to compare state laws on a single subject.
Imagine a three-column table with a drop-down box on the top. A viewer
selects a state from the list, and that state's text fills the...
|
by: Albert Ahtenberg |
last post by:
Hello,
I have two questions.
1. When the user presses the back button and returns to a form he filled
the form is reseted. How do I leave there the values he inserted?
2. When the...
|
by: inderjit S Gabrie |
last post by:
Hi all
Here is the scenerio ...is it possibly to do this...
i am getting valid course dates output on to a web which i have designed
....all is okay so far , look at the following web url
...
|
by: Jack |
last post by:
Hi All,
What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g.
select x from y where z=:parameter
Which in asp/jsp would be followed by some statements to bind a value...
|
by: Sandwick |
last post by:
I am trying to change the size of a drawing so they are all 3x3.
the script below is what i was trying to use to cut it in half ... I
get errors.
I can display the normal picture but not the...
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |