File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Just a Simple Java Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Just a Simple Java Question" Watch "Just a Simple Java Question" New topic
Author

Just a Simple Java Question

Kareem Adams
Greenhorn

Joined: Sep 22, 2007
Posts: 6
Hi there,

I'm just learning Java and playing around this simple code (it sums up the total of numbers in a range)


It prints sum of 1 to 10. But I don't quite understand the logic in the sum().

If, sum = sum + i;

How does it arrive at the calculation?

sum was initially 0 and i = x = 1 from the above

Then it should read:

initially: sum = sum + 1
x = 1
level 1: 0 = 0 + 1 - initial state, so sum gets to be 1

x = 2
1 = 1 + 2 (1 increments to become 2), so sum gets to be 3 and so on.

This is the logic I'm trying to understand.

Can someone please shed more light on this for me or give it a better explanation.

Thank you.





Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7029
    
  16

Kareem Adams wrote:...
x = 2
1 = 1 + 2 (1 increments to become 2), so sum gets to be 3 and so on.

This is the logic I'm trying to understand.

Can someone please shed more light on this for me or give it a better explanation.

I'm not quite sure what it is you're having problems with because, apart from the fact that it's i that gets incremented, not x, your description seems to be right on the mark.

If need be, just continue the process until i == 10 and work out what sum should contain.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3160
    
  10
Kareem Adams wrote:This is the logic I'm trying to understand.

Can someone please shed more light on this for me or give it a better explanation.

You seem to have explained it perfectly. What exactly about your explanation don't you understand ?

Joanne
Kareem Adams
Greenhorn

Joined: Sep 22, 2007
Posts: 6
Thanks to you guys, here's what I did.



Doing it manually makes me understand maybe a little (technically).

But the part I really want to get right is in simple English.

How does that part work?

Because what it does is : 1+2+3+4+5+6+7+8+9+10

If I get this right, I'll be able to right something similar with better understanding without looking at any book.
The code I sent earlier was from a book I'm using to study.

And thanks to Matthew for formatting my code earlier.

I appreciate y'all effort.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7029
    
  16

Kareem Adams wrote:But the part I really want to get right is in simple English.

How does that part work?

It works because ints in Java are modifiable, so the statement says:
take the value of sum, add i to it, and then use that result to modify sum.

Everything on the right-hand side of the '=' sign is based on the old value of sum, but after the statement has completed, sum will be updated with the new value.

In some languages (eg, C/C++) these are called 'lvalue's and 'rvalue's ('l' and 'r' being short for 'left' and 'right').

HIH

Winston
Kareem Adams
Greenhorn

Joined: Sep 22, 2007
Posts: 6
take the value of sum, add i to it, and then use that result to modify sum.

Everything on the right-hand side of the '=' sign is based on the old value of sum, but after the statement has completed, sum will be updated with the new value.



This explains it better.


Thanks Winston.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Just a Simple Java Question
 
Similar Threads
Trying to get manageable arrays to work with
text file extractor
Table Viewer GUI from a Database
Tidying up my code?
The "this" keyword and the constructor.