• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how does this add up to 15?

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry guys just cant work out how this sums up to 15
just need some explanation please

     
 
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is 1+2+3+4+5 = 15

What you are doing here is saying my sum starts at 0 and my counter starts at 1. add my counter to my sum. Repeat until my counter is greater than 5.

So you're adding 1 through 5.

Is there a specific operator you're confused on?

-Zach
 
Zach Rode
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
* add my counter to my sum THEN increase my counter by 1.

Sorry I don't think the forums let me edit
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zach Rode wrote:. . . add my counter to my sum THEN increase my counter by 1.

Well, sort of. Because the i++ operator has the highest precedence, you have to regard it as being executed first, but it is applied only to i. This sounds confusing, but there are actually two values: the value of i and that of the whole expression i++. But the value of i++ is the same as the old value of i. So the five values are 1 2 3 4 5, as you said, adding up to 15.

Sorry I don't think the forums let me edit

We had trouble with edits in the past; if you really need an edit ask one of us mods.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody has pointed out I wasn't precise about the evaluation order. Sorry. The operator on the left (+=) starts before the ++ operator, but i++ will supply its result to +=. That result is equal to the old value of i.
 
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The output:

 
reply
    Bookmark Topic Watch Topic
  • New Topic