473,657 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

meaning of j++

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"

Aug 3 '06 #1
11 9538
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.

Aug 3 '06 #2
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
Aug 3 '06 #3

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

Aug 3 '06 #4
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
Aug 3 '06 #5
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::operator++ (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::operator++ ()

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
Aug 3 '06 #6
2 Thomas-es are really great :-)

(i mean Thomas & Thomas J. Gritzen)

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

thanks.

"arnuld"

Aug 3 '06 #7
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"

Aug 3 '06 #8
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
Aug 3 '06 #9
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"

Aug 3 '06 #10

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

Similar topics

5
6084
by: Richard Cornford | last post by:
I am interested in hearing opinions on the semantic meaning of FORM (elements) in HTML. I have to start of apologising because this question arose in a context that is not applicable to the Internet. Specifically, marking up a document that will contain multiple related form controls (intended exclusively for client-side scripting) that would never be intended to be submitted. I realise that that concept is a non-starter in an Internet...
7
27198
by: John Baker | last post by:
HI: I see the term "Me!" in a lot of Access statements, bit don't quite know what it means. In the same contexts as wondering what the meaning if "is" is, I wonder what the meaning of "Me!" is. Can someone enlighten me? Regards
4
3683
by: RF Emmerink | last post by:
Hi, A simple question: What is the meaning of: variable >> 1 Kind regards,
388
21663
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
19
3845
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
87
5111
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for the library, and other resources related to managed strings are available for download from the CERT web site at: http://www.cert.org/secure-coding/managedstring.html
232
13242
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
5
1608
by: =?Utf-8?B?Sm9seW5pY2U=?= | last post by:
Hi everyone, I am learning c# and i don´t understand a piece of code , can anyone explain the meaning of this code please. public class vector { public double? R = null; //I suppose this is a Generic Type ??? ..........
43
17199
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
1
2134
oranoos3000
by: oranoos3000 | last post by:
hi would you please help me? i dont know correct meaning of cash webpage? meaning of cash web page is that pages is saved on web server and for next time that is requeste the same page server immediately send the same page that save in it's memory or meaning of cash web page is that some of files is save in local computer and next time that user type the same url in location bar requested page returned to it immediately and dont need...
0
8844
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...
1
8518
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
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5643
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
4173
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
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
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.