# Return from incrementing for loop?

Printable View

• October 3rd, 2010, 02:22 PM
mindlessn00b
Return from incrementing for loop?
I'm trying to return a variable form a for loop that is incrementing
here is my for loop i need to return the sum value (i know i need to change void from the method name)
Code Java:

``` public static void loopSum(int n){ int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + i;```

i need to return the value of 5050 ("n" is set to 100 in the rest of the code, i just can't seem to figure this out.
I hope i posted this in the right place. Thanks in advance!
• October 3rd, 2010, 02:30 PM
Norm
Re: Return from incrementing for loop?
The method you are in does NOT return anything. It is defined as void. If you change it to return an int, then you should be able to return a value:
return 5050;

Where does the value 5050 come from?

You need to post all of the code that you are talking about.
• October 3rd, 2010, 02:36 PM
mindlessn00b
Return from incrementing for loop HELP?!
Code java:

```public class IntSumReturnValues {   public static void main(String[] args) { int n = 100; // int sumLoop= System.out.println("The sum of the numbers from 1 to " + n + " is " + loopSum(n) + ". Calculated with a loop"); // print out the "n" value and the loopSum value of 5050   System.out.println("The sum of the numbers from 1 to " + n + " is " + formulaSum(n) + ". Calculated with the formula."); // print out the "n" value and the formulaSum value of 5050     } public static int loopSum(int n){ int sum = 0; for (int i = 1; i <= n; i++) {//increment "i" to 100(n) int loopSum = sum + i; // had a println where return is and used "loopSum" to print out the value but i cant return it now what am i doing wrong?       return loopSum(n); } } public static int formulaSum(int n) { int formulaSum = n * (n+1) / 2; return formulaSum; } }```
I cant seem to figure out how to return the loop sum value to just do a simple print out of the value it should be 5050.
• October 3rd, 2010, 02:41 PM
mindlessn00b
Re: Return from incrementing for loop?
Yes thank you i know i need to change the method to int so i can return an Integer. i reposted the whole code in the "whats wrong with my code" forum the value of sum from what i linked when i use it in a println it is 5050. My teacher is trying to prove that a formula can be used instead of loops in certain instances to reduce the redundancy.
• October 3rd, 2010, 03:09 PM
copeg
Re: Return from incrementing for loop?
Please do not repetitively ask the same question in multiple threads, and please use the code tags to make your code more readable. I have locked your other identical post, and merged the third with this.
• October 3rd, 2010, 03:12 PM
Norm
Re: Return from incrementing for loop?
What does the program print out when you execute it?
• October 3rd, 2010, 03:19 PM
mindlessn00b
Re: Return from incrementing for loop?
this is what i get for errors
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type int

at IntSumReturnValues.loopSum(IntSumReturnValues.java :14)
at IntSumReturnValues.main(IntSumReturnValues.java:7)
also have a little notation for the "i++" saying it is a dead code....
is the return taking it out of the incrementing process?
• October 3rd, 2010, 03:24 PM
Norm
Re: Return from incrementing for loop?
What don't you understand about the error message?

Don't know what your IDE does. Does the notation make sense?
• October 3rd, 2010, 03:36 PM
mindlessn00b
Re: Return from incrementing for loop?
I'm very new to programming and my teacher is very vague he just makes slide shows and they are all very unorganized this has been a problem with his classes for years from what i hear. So honestly i have not idea what it means I dont understand that when i put in the return it makes the i++ become dead unless the return is infact stopping the incrementing. and if that is the case i have no idea how to get the outcome to be returned.
• October 3rd, 2010, 03:45 PM
Norm
Re: Return from incrementing for loop?
Play computer with your code. Do each statement one at a time using a piece of paper to keep track of the values of variables.
See if the i++ part of the for loop control is executed.

Why is the return inside of the loop?
• October 3rd, 2010, 03:55 PM
mindlessn00b
Re: Return from incrementing for loop?
I'm not really sure he hasn't gone over returns yet... he just assigned a homework assignment with it in it. I read a little on returns but still couldn't get it. It worked in the second method "formulaSum" so i thought it might have worked in the first one but I'm stuck because of the i++ and it saying that int loopSum isn't and integer?
• October 3rd, 2010, 04:01 PM
Norm
Re: Return from incrementing for loop?
I have no more to say than past what I said back in post#10.

Quote:

it saying that int loopSum isn't an integer
Please copy full text of error message and paste it here.
• October 3rd, 2010, 04:11 PM
mindlessn00b
Re: Return from incrementing for loop?
Okay, well thank you Norm for your help. I've changed the variable for "= sum+ i " to sumLoop and changed the return as well still having the same issue.

I'm assuming because after it goes through the loop once it is trying to return the value. So i tried taking the return out of the for loop but leaving it in the method and it cant identify the variable i chose to use cause it is out of the loop. I really have no clue where to go from here?
Code Java:

```public static int loopSum(int n){ int sum = 0; for (int i = 1; i <= n; i++) {//increment "i" to 100(n) int sumLoop = sum + i; // had a println where return is and used "loopSum" to print out the value but i cant return it now what am i doing wrong?         }return sumLoop; }```
• October 3rd, 2010, 04:30 PM
Norm
Re: Return from incrementing for loop?
Why do you have the variable: sum and the variable sumLoop?
You need to move the definition of sumLoop out of the {} the same as the definition for sum if you want to be able to access its value outside of the loop.

What will the value of sumLoop be when the loop is done executing?
• October 3rd, 2010, 04:34 PM
mindlessn00b
Re: Return from incrementing for loop?
oh wow it was that simple!!!!! Thanks a ton Norm!!!!
Code Java:

```public class IntSumReturnValues {   public static void main(String[] args) { int n = 100; // int sumLoop= System.out.println("The sum of the numbers from 1 to " + n + " is " + loopSum(n) + ". Calculated with a loop"); // print out the "n" value and the loopSum value of 5050   System.out.println("The sum of the numbers from 1 to " + n + " is " + formulaSum(n) + ". Calculated with the formula."); // print out the "n" value and the formulaSum value of 5050     } public static int loopSum(int n){ int sum = 0; for (int i = 1; i <= n; i++) {//increment "i" to 100(n) sum = sum + i; // had a println where return is and used "loopSum" to print out the value but i cant return it now what am i doing wrong?         }return sum; } public static int formulaSum(int n) { int formulaSum = n * (n+1) / 2; return formulaSum; } }```
it was that simple haha it works now again thank you!
• October 3rd, 2010, 04:38 PM
Norm
Re: Return from incrementing for loop?
Glad you got it working.
Good luck on the next project.
• October 4th, 2010, 12:15 PM
Norm
Re: Return from incrementing for loop?
@daviddwilson
Where are the rest of your posts? This post looks like it has lost its way and has ended up in the wrong thread.

I think I recommended that you play computer and step through your code statement by statement to see if the i++ COULD EVER be executed.
Did you do that? Do you think the i++ can be executed, ever. That is what your IDE is telling you: That code CAN NEVER be executed. Hence is dead/useless/misleading etc.