for loop

Feb 15, 2013 at 6:38pm
I'm very new to c++. Just a question: in a for loop, is it possible to funnel the previous RESULT of the loop back through the loop? This is probably a very simple thing to do, but I'm obviously over-thinking it.
Feb 15, 2013 at 6:43pm
Not sure what you mean. What exactly do you mean by "RESULT"?
Feb 15, 2013 at 6:44pm
As you're new to C++, I don't know how much you know. But if you use a function, this can be done easily.

If you want the result of a loop to go through the same loop, you'll have to have two loops, one after another. Just, in the second loop, use the variable that was changed in the first.

Hope this helps (:
Feb 15, 2013 at 6:48pm
@Peter87 - sorry about not being so clear. But I think Lynx876 got it.
@Lynx876 - Thank you! This does help. Another question: if you want the result to go through more than twice, say, 20 times, is there a way to do that simpler than typing out the loop 20 times?
Feb 15, 2013 at 7:00pm
You can put the loop inside another loop.
1
2
3
4
5
6
7
8
9
int sum = 0;
for (int i = 0; i < 20; ++i)
{
	for (int j = 0; j < 5; ++j)
	{
		sum += j;
	}
	std::cout << sum << std::endl;
}
Feb 15, 2013 at 7:03pm
Thank you!!!
Feb 15, 2013 at 7:04pm
aha! I didn't think of nested loops! Sorry.
Topic archived. No new replies allowed.