• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Preincrement

 
A. Singh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bracketing with () is redundant; it makes no difference to result.

And welcome to the Ranch
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it makes it easier to read.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
A. Singh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic