1. ## summation in c

Hi, I'm relatively new to c, and understand most of the basics, but I'm have a problem with while and for loops in trying to implement an arithmetic series.

I thought something like:

Code:
```float sum=0.0;
for(x=0;x<=5;x++)
{
sum+=a+(b*x);
return sum;
}
printf("sum of series = %f\n",sum)```
-where variables have been declared before hand - would produce the equivalent of the mathematical series:

sum=a+(a+b)+(a+2b)+(a+3b)+(a+4b)+(a+5b)

..since everything I have read about for loops, says that the block of code between the curly parenthesis is executed repeatedly, for each value of x (startign at 0 and incrementing by one per iteration) , until the condition x<=5 is true.

By having the variable 'sum' that is initially 0, incrementing this value by 'a+(b*x)' for each iteration, and using 'return sum;' at the end of each iteration, I would have thought that on each loop between x=0 and x=5, the value a+xb would be added to 'sum' and then this new value returned back to the program, becoming the new value of 'sum' for the next iteration...such that when the loop has finished, the above series is yielded.

I have compiled this code sucessfully, and yet when I run it there is no output....i.e nothing gets printed.

What is wrong with this? thanks

2. return always immediately stops execution of the current function. Edit: which is to say, take it out and we'll all be happy.

3. Summation = for loop.

I really tried hard to get a nice looking ascii art thing going on to show you... but its no use. If only the forum supported MathML.... *ahem* Kermi... if only...

4. Originally Posted by tabstop
return always immediately stops execution of the current function.
Oh I see, I thought 'return (); was like to reset the value of sum acoording to previous changes to it (such as incrementation).

So how can I acheive this? Is there a command to use in place of 'return' that will do the job I'm looking for, or does it have to be done some other way? thanks for the replies

5. Originally Posted by bertazoid
Oh I see, I thought 'return (); was like to reset the value of sum acoording to previous changes to it (such as incrementation).
Since doing so would completely undermine the whole project, I doubt you want to reset the value of sum. What you want to do is "nothing", which involves restraining yourself from typing a line of code (and in this case, deleting a line of code that does "something" instead).

6. If you find you cannot live without an extra line of code:

Example:
Code:
```float sum=0.0;
for(x=0;x<=5;x++)
{
sum+=a+(b*x);
continue;

/* You could even put whatever you want down here. It will be skipped :) */
}
printf("sum of series = &#37;f\n",sum)```

7. Of course, if what you want to print the series, you just have to replace that return with a call to printf.

8. Originally Posted by tabstop
Since doing so would completely undermine the whole project, I doubt you want to reset the value of sum. What you want to do is "nothing", which involves restraining yourself from typing a line of code (and in this case, deleting a line of code that does "something" instead).
Ah, Ok thanks. When I said 'reset' I didn't mean return to 0, I meant change variable sum from what it was preiviously to sum+=a+(b*x), and 'save' the variable sum as this new value, ready for the next iteration...and incrementation/addition....doesn't matter anyway, as I obviously didn't properly uderstand 'return' anyway .

9. Code:
```float sum=0.0;
for(x=0;x<=5;x++)
{
sum+=a+(b*x);
printf("\r&#37;f", sum);
}
printf("\rsum of series = %f\n",sum)```

10. Originally Posted by bertazoid
Ah, Ok thanks. When I said 'reset' I didn't mean return to 0, I meant change variable sum from what it was preiviously to sum+=a+(b*x), and 'save' the variable sum as this new value, ready for the next iteration...and incrementation/addition....doesn't matter anyway, as I obviously didn't properly uderstand 'return' anyway .
+= always always always always always always always always changes the left-hand side.

11. Originally Posted by IceDane
Of course, if what you want to print the series, you just have to replace that return with a call to printf.
Hmmm, do you mean to use 'printf("%f\n")' within the curly parenthesis, or without? I think I only need to use it at the end to display the answer, but i guess if i put it inside as well, it would return a list of the terms, before the answer, right?

12. Originally Posted by bertazoid
Hmmm, do you mean to use 'printf("%f\n")' within the curly parenthesis, or without? I think I only need to use it at the end to display the answer, but i guess if i put it inside as well, it would return a list of the terms, before the answer, right?
Look at my post, homeboy. tabstop merely corrected your assertion about how the += operator works.

13. Originally Posted by tabstop
+= always always always always always always always always changes the left-hand side.
thanks, I will remember that for future..I think i was snoozing when our lecturer told us that.

14. Originally Posted by bertazoid
thanks, I will remember that for future..I think i was snoozing when our lecturer told us that.
Well, in general, when you assign something to something, it changes immediately. I can see what you mean though, but no, return is not needed to 'apply the changes'.

15. Are you an SQL programmer, bertazoid? That is the sort of behavior tables exhibit.