This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Preincrement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Preincrement" Watch "Preincrement" New topic
Author

Preincrement

A. Singh
Greenhorn

Joined: Jun 07, 2011
Posts: 2
I am starting to learn Java and I am currently learning about pre-increment. This is the lesson:

Shortcut: increments and decrement operators
These symbols act as automatic 1 adders or subtractors.
++ adds one to something, - - subtracts one from something.
There are two ways to use these. Here is one:

After setting the initial number of slugs at 27, we increase it by one with
++number of slugs;
and then we print 28, and then increase it again to 29 with
out.println(++numberofslugs);
and then it gets printed as 29, and then one last time we print it as 29 to show the final value.
Try the slug program.
TO BE HANDED IN:
Now change it to another animal, show the initial value and run it up by four with this method, showing
each increase.


Now, I am having problems with making the initial value increase by four. I tried many methods, including "+4numberofslugs" and "numberofslugs = ++numberofslugs + ++numberofslugs + ++numberofslugs + ++numberofslugs" but nothing works. I know a way to make it work using postincrement, but this assignment requires only that I use preincrement, so if anyone can help me with this problem, I'll really appreciate it.

Thanks.
Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

Hi, if you really want that using increment operators and that too in one statement, I would suggest you to brace them!

numberofslugs = (++numberofslugs) + (++numberofslugs) + (++numberofslugs) + (++numberofslugs);

I feel this should do it!

Thanks,
Pavan.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

"Nothing works" really doesn't tell us much...

+4numberofslugs is not valid java syntax - or at least, it doesn't do what you want. It won't add four to the value.

I also don't think Pavan's solution would work.

You may simply want a loop that calls the "print the value", "print the pre-incremented value", and "print the value" code four times.

edit: Pavan's solution does not work.



gives an output of

28
29
29
126

the 126 being the sum of 30,31,32 and 33

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
The bracketing with () is redundant; it makes no difference to result.

And welcome to the Ranch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

But it makes it easier to read.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

This is a simple assignment With a line like numberofslugs = (++numberofslugs) + (++numberofslugs) + (++numberofslugs) + (++numberofslugs); you are way overcomplicating the problem.

Carefully think about what the pre-increment operator does. If you write ++numberofslugs, this means: increment the value of the variable numberofslugs by one and return the new value. You don't need to do any assignment; just writing ++numberofslugs will increment the value of the variable by itself. In fact, using assignment and the pre- or post-increment operator together is a common source of misunderstanding with beginnning Java programmers.

Note that the assignment does not ask you to increment the value by 4 in one go. On the contrary, it says "show the initial value and run it up by four with this method, showing
each increase
" which means you have to increment it one by one, printing the value each time you do an increment.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
A. Singh
Greenhorn

Joined: Jun 07, 2011
Posts: 2
Jesper de Jong wrote:This is a simple assignment With a line like numberofslugs = (++numberofslugs) + (++numberofslugs) + (++numberofslugs) + (++numberofslugs); you are way overcomplicating the problem.

Carefully think about what the pre-increment operator does. If you write ++numberofslugs, this means: increment the value of the variable numberofslugs by one and return the new value. You don't need to do any assignment; just writing ++numberofslugs will increment the value of the variable by itself. In fact, using assignment and the pre- or post-increment operator together is a common source of misunderstanding with beginnning Java programmers.

Note that the assignment does not ask you to increment the value by 4 in one go. On the contrary, it says "show the initial value and run it up by four with this method, showing
each increase
" which means you have to increment it one by one, printing the value each time you do an increment.


The brackets are redundant; they made no difference (even if it made it easier to read). Also, my teacher provided me with this assignment and he told me that I had to increase the value of the initial value by four (not 1 by 1).
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

Pure Ownage wrote:Also, my teacher provided me with this assignment and he told me that I had to increase the value of the initial value by four (not 1 by 1).

According you your initial post:
Now change it to another animal, show the initial value and run it up by four with this method, showing
each increase.

the "with this method" to me says "use the method shown above", which is the pre-increment operator. The "showing each increase" implies that he/she wants you to show the results after each of the four "+1" increases.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Preincrement