6 Replies - 272 Views - Last Post: 13 October 2012 - 07:47 AM Rate Topic: -----

#1 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Increments

Posted 13 October 2012 - 06:15 AM


++z
z++




Can some please tell me the difference between the two?
Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Increments

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Increments

Posted 13 October 2012 - 06:17 AM

Post-increment : add 1 to the value.
The value is returned before the increment is made, e.g.
x = 1;
y = x++;
Then y will hold 1 and x will hold 2


Pre-increment : add 1 to the value.
The value is returned after the increment is made, e.g.
x = 1;
y = ++x;
Then y will hold 2 and x will hold 2.
Was This Post Helpful? 1
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Increments

Posted 13 October 2012 - 06:17 AM

You've been around long enough to be able to find this basic info by yourself. Google "java operators" and navigate to the Oracle Tutorials page which has a nice trail on the topic.
Was This Post Helpful? 1
  • +
  • -

#4 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: Increments

Posted 13 October 2012 - 07:30 AM

int z = 5;
if(++z > 5 || ++z > 6) z++ // z = 7



I want to see if i understand the above.

The ++z adds 1 to the 5 making it 6 then the z++ at the end adds another 1 making the number 7?
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Increments

Posted 13 October 2012 - 07:36 AM

Hopefully, this is a nonsensical homework question and not something you've actually coded, because there's no reason - other than academic exercise - to obscure code's real purpose with such useless complexity.
Was This Post Helpful? 0
  • +
  • -

#6 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: Increments

Posted 13 October 2012 - 07:41 AM

Thats right the code above was an example used the the study book to explain the difference between the two operators

This post has been edited by BrendanH: 13 October 2012 - 07:43 AM

Was This Post Helpful? 0
  • +
  • -

#7 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Increments

Posted 13 October 2012 - 07:47 AM

Here, I will write you a little example to try to explain it. When you write ++, it is called increment -- is decrement. If you do ++ before a variable name it increments the variable by 1 before it running it. If you were to do a variable then ++ then it would increment it after here is an example:
int x = 10;

System.out.println(++x);

// This would print out 11 because even though x is 10, it is incremented before printing it.

System.out.println(x++);

/* If I were to write this it would print out 10, but then next I printed x it would be 11 because  * it is incremented after printing it.
* All the same applies to -- as that is just doing minus 1 instead of plus 1
*/



Hope this helped.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1