Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > meaning of j++

Reply
Thread Tools

meaning of j++

 
 
arnuld
Guest
Posts: n/a
 
      08-03-2006
i have just started to learn C++ from Bruce Eckel's book "Thinking in
C++ 2/e" & i am trying to understand the meaning of "j++". ++j is
pretty clear to me. i am using "g++" on my Debian Sarge.

here is the problem:

i = j = k = l = 0

++i -> --i = 0 ;; OK, fine

j++ -> j-- = 1 ;; WHAT?, why not 0

k++ -> k++ = 1 ;; WHAT?, why not 2.

l-- = -> l-- = -1 ;; why not -2


no i am not talking about efficiency/optimisation. i am talking about
the answers i got.

I searched archives but all i got is optimisations regarding the use of
++j & j++ but not regarding meaning of these. does anybody have any
idea? Bruce did not explain this behaviour in his book (except for a
single sentence)

thanks

"arnuld"

 
Reply With Quote
 
 
 
 
loufoque
Guest
Posts: n/a
 
      08-03-2006
arnuld wrote :

> j++ -> j-- = 1 ;; WHAT?, why not 0


because j-- returns the old value, not the new one.

>
> k++ -> k++ = 1 ;; WHAT?, why not 2.


because k++ returns the old value, not the new one.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-03-2006
arnuld wrote:
> i have just started to learn C++ from Bruce Eckel's book "Thinking in
> C++ 2/e" & i am trying to understand the meaning of "j++". ++j is
> pretty clear to me. i am using "g++" on my Debian Sarge.
>
> here is the problem:
>
> i = j = k = l = 0
>
> ++i -> --i = 0 ;; OK, fine
>
> j++ -> j-- = 1 ;; WHAT?, why not 0
>
> k++ -> k++ = 1 ;; WHAT?, why not 2.
>
> l-- = -> l-- = -1 ;; why not -2


We're in a C++ language newsgroup. The text above is not C++. Care
to explain what it is you mean by all those valid C++ tokens mixed up
in a strange order?

> [..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Thomas
Guest
Posts: n/a
 
      08-03-2006

arnuld wrote:
> i have just started to learn C++ from Bruce Eckel's book "Thinking in
> C++ 2/e" & i am trying to understand the meaning of "j++". ++j is
> pretty clear to me. i am using "g++" on my Debian Sarge.
>
> here is the problem:
>
> i = j = k = l = 0
>
> ++i -> --i = 0 ;; OK, fine
>
> j++ -> j-- = 1 ;; WHAT?, why not 0
>
> k++ -> k++ = 1 ;; WHAT?, why not 2.
>
> l-- = -> l-- = -1 ;; why not -2
>
>
> no i am not talking about efficiency/optimisation. i am talking about
> the answers i got.
>
> I searched archives but all i got is optimisations regarding the use of
> ++j & j++ but not regarding meaning of these. does anybody have any
> idea? Bruce did not explain this behaviour in his book (except for a
> single sentence)
>
> thanks
>
> "arnuld"


This can be confusing, but is really easy!

The post increment operator like j++ maps to a funtion like this:

int operator++()
{
int tmp = j
j += 1;
return tmp;

}

And the pre incremend operator maps to a function like this:
int operator++(int)
{
j += 1;
return j;
}

If the function names confuses you don't pay attention to them, but see
what they do.

So j++ return the old value of j , but increases j by 1, and ++j
increases first and then return the value of j.


-Thomas

 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      08-03-2006
arnuld schrieb:
> i have just started to learn C++ from Bruce Eckel's book "Thinking in
> C++ 2/e" & i am trying to understand the meaning of "j++". ++j is
> pretty clear to me. i am using "g++" on my Debian Sarge.
>
> here is the problem:
>
> i = j = k = l = 0
>
> ++i -> --i = 0 ;; OK, fine
>
> j++ -> j-- = 1 ;; WHAT?, why not 0
>
> k++ -> k++ = 1 ;; WHAT?, why not 2.
>
> l-- = -> l-- = -1 ;; why not -2


++j and j++ both do the same thing: They increment the variable j.

The difference is the _value_ of the expressions:

++j is called prefix-increment, it increments j and returns the
incremented value. So ++j equals the value of j after the increment
operation.

j-- is called postfix-increment. It increments j but its value is that
of j before the increment.

Example:

int j, a, b, c;

j = 0;
a = ++j; // a and j are 1
b = j++; // b is 1, but j is 2
c = j; // c is 2

--
Thomas
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-03-2006
Thomas wrote:
> arnuld wrote:
>> i have just started to learn C++ from Bruce Eckel's book "Thinking in
>> C++ 2/e" & i am trying to understand the meaning of "j++". ++j is
>> pretty clear to me. i am using "g++" on my Debian Sarge.
>>
>> here is the problem:
>>
>> i = j = k = l = 0
>>
>> ++i -> --i = 0 ;; OK, fine
>>
>> j++ -> j-- = 1 ;; WHAT?, why not 0
>>
>> k++ -> k++ = 1 ;; WHAT?, why not 2.
>>
>> l-- = -> l-- = -1 ;; why not -2
>>
>>
>> no i am not talking about efficiency/optimisation. i am talking about
>> the answers i got.
>>
>> I searched archives but all i got is optimisations regarding the use
>> of ++j & j++ but not regarding meaning of these. does anybody have
>> any idea? Bruce did not explain this behaviour in his book (except
>> for a single sentence)
>>
>> thanks
>>
>> "arnuld"

>
> This can be confusing, but is really easy!
>
> The post increment operator like j++ maps to a funtion like this:
>
> int operator++()


You meant

int int:perator++(int)

didn't you?

> {
> int tmp = j

;

> j += 1;
> return tmp;
>
> }
>
> And the pre incremend operator maps to a function like this:
> int operator++(int)


You meant

int& int:perator++()

didn't you?

> {
> j += 1;
> return j;
> }
>
> If the function names confuses you don't pay attention to them, but
> see what they do.
>
> So j++ return the old value of j , but increases j by 1, and ++j
> increases first and then return the value of j.


Actually ++j returns _a reference_ to the old object 'j' with the new value.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-03-2006
2 Thomas-es are really great

(i mean Thomas & Thomas J. Gritzen)

both explained in a very-easy way & i understood.

thanks.

"arnuld"

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-03-2006
Victor Bazarov wrote:

> We're in a C++ language newsgroup. The text above is not C++. Care
> to explain what it is you mean by all those valid C++ tokens mixed up
> in a strange order?


i thought of putting the entire programme but that could clutter the
post, that is why i used arrows.

BUT Victor, you are here from a long-time, so you know better. if you
say "always in the form of a programme" then i will do so. what do you
say?

"arnuld"

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-03-2006
arnuld wrote:
> Victor Bazarov wrote:
>
>> We're in a C++ language newsgroup. The text above is not C++. Care
>> to explain what it is you mean by all those valid C++ tokens mixed up
>> in a strange order?

>
> i thought of putting the entire programme but that could clutter the
> post, that is why i used arrows.
>
> BUT Victor, you are here from a long-time, so you know better. if you
> say "always in the form of a programme" then i will do so. what do
> you say?


We should strive to use two languages here: C++ and English. When I see

j++ -> j++ = 1

what should I think? Yes, I've been here a while, so I can probably
guess that you meant

j++;
cout << j++; // outputs 1

but what somebody without enough experience should make of it? When you
post, you shouldn't just think that your questions are read (and well
understood) by somebody who is experienced. You should think of somebody
with less experience than yourself and make sure that both your question
and the answers are clear.

BTW, was it so difficult to write

j++, cout << j++; // outputs 1 -- some comment

instead of

j++ -> j++ = 1 ;; some comment

???

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-03-2006
Victor Bazarov wrote:


> We should strive to use two languages here: C++ and English.


hey, hey making fun of me by saying " and English" i like it

> When I see
>
> j++ -> j++ = 1
>
> what should I think? Yes, I've been here a while, so I can probably
> guess that you meant
>
> j++;
> cout << j++; // outputs 1


i meant:

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


> but what somebody without enough experience should make of it? When you
> post, you shouldn't just think that your questions are read (and well
> understood) by somebody who is experienced. You should think of somebody
> with less experience than yourself and make sure that both your question
> and the answers are clear.


whoopy.....! I thought *I* was least experienced. WOW i am wrong.

> BTW, was it so difficult to write
>
> j++, cout << j++; // outputs 1 -- some comment
>
> instead of
>
> j++ -> j++ = 1 ;; some comment


nope, i did write that. my mistake

> V





> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask



what do you mean by "top-posted replies". asking since i dont know.

"arnuld"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding search queries, semantics, and "Meaning" ...aren't weall looking for meaning? 5lvqbwl02@sneakemail.com Python 4 01-14-2009 02:28 PM
use work.my_package.all-->what exactly meaning of this Parthav VHDL 4 01-13-2006 07:50 PM
WHAT IS THE MEANING *FESWANY MCSE 3 01-28-2005 02:55 PM
Meaning of word "develop" in the exam (70-300) TomTom MCSD 10 08-13-2004 04:39 PM
Meaning of output value? Sander Odekerken VHDL 2 05-18-2004 05:41 PM



Advertisments