Results 1 to 7 of 7
Thread: Pre- and post-increments
- 10-29-2009, 02:25 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
Pre- and post-increments
Ok, so I read the related section at the Java site about Assignment, Arithmetic, and Unary Operators, but I honestly don't get the code they provide:
I understand that i++ adds 1 to the original value, and ++i adds 1 to the incremented value.Java Code:class PrePostDemo { public static void main(String[] args){ int i = 3; i++; System.out.println(i); // "4" ++i; System.out.println(i); // "5" System.out.println(++i); // "6" System.out.println(i++); // "6" System.out.println(i); // "7" } }
When i++ prints '6', does that mean the original value of i=3 is changed to 5 at this point?
I also don't get why i=7 at the end.
-
Do you have a book chapter on this? If so, you should re-read it as again your assumptions about your understanding is incorrect. Both i++ and ++i add to the original value. ++i adds to the int before it is used and i++ adds to it after it has been used. Again, this will probably be better explained in your book.
Last edited by Fubarable; 10-29-2009 at 02:46 AM.
- 10-29-2009, 02:47 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
The Java class I am taking unfortunately does not provide us with a book so I am stuck with Powerpoint slides that aren't really helpful. I might go out and buy a book I guess.
-
I would agree with this 100%. When I'm learning a new programming language, I usually go out and buy several books because it is tough getting these new concepts into my head. Many of the books I buy are used, so they're obtained on the cheap. Also, you should check out the Sun Java tutorials which are better than cheap -- they're free. You'll find them here: Sun Java Tutorial Big Index
The section on the incr and decr operators can be found here: operatorsLast edited by Fubarable; 10-29-2009 at 02:53 AM.
- 10-29-2009, 03:02 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
I'll check out those links. And thank-you very much for your advice. :D
-
To go through your code:
Java Code:public static void main(String[] args){ int i = 3; i++; // add one to i System.out.println(i); // display this number ++i; // add again to i System.out.println(i); // display this number System.out.println(++i); // first add one to i, THEN display it. System.out.println(i++); // first display i, THEN after it's been displayed, add one to it. System.out.println(i); // "7" }
- 10-29-2009, 03:29 AM #7
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
DOnt know if 1st post if did, I am VERY sorry for duplicate post. I have error messg
By afisher300 in forum New To JavaReplies: 3Last Post: 05-04-2009, 03:15 AM -
my first post
By kaviyasivashanmugham in forum New To JavaReplies: 2Last Post: 03-13-2009, 09:50 AM -
post count
By codeflip in forum Forum LobbyReplies: 2Last Post: 03-12-2009, 02:20 AM -
First post out of the way..
By sirwiggles in forum IntroductionsReplies: 0Last Post: 02-06-2009, 10:44 PM -
First post ever
By pbpersson in forum IntroductionsReplies: 4Last Post: 08-16-2008, 05:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks