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.
public class demoLoops2
{
public static void main(String[] args)
{
int counter=0;
int sum=0;
for (counter = 1; counter <=10; counter +=2) sum = sum + counter;// Loop with incrementor
{
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....
Can someone please help explain this to me.
Thanks,
Nick