My code is working correctly, but I don't know why it is working correctly...

Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

My code is working correctly, but I don't know why it is working correctly...

Hello all,

I am new to forum so first of all I would like to introduce myself. My name is Nick and I work for a software company. I supersize a team of software testers. I am not a coder myself, but have learned a lot in the last couple years of testing code. I am currently taking a intro to java class and we are now doing projects on loops. The project I am doing now is to get a sum of all of the off numbers in the loop depending on the input a user enters.

So I know how to setup the part where the users input a variable and am familiar with the try and catch section for exceptions...

This is my code so far without those sections included and just uses a predetermined value for my limit.

System.out.println("This is the sum of the odd numbers in the range " + sum);

}//End of the multi statement for loop

The sum ends up being correct at 25 which would be 1+3+5+7+9=25. My question is how did my variable sum go from 0 to 25? The line of code I wrote : sum = sum + counter; to me means sum (0) = sum (0) + counter (which at that point I'm sure now what the counter value is. Is the counter value 9 because 9 is the last integer it can get to before it sets the loop false? In that case it would be 0 = 0 + 9... I'm so confused as to how I am arriving at the correct answer....