Can anyone lease tell me the value of k? I know it starts at 0 and loops backwards but I can't figure out where it terminates. I would really appreciate someone telling me the best way to figure this out so I can solve these problems in the future

int k = 0;
for (int i = -1; i <= 3; i++)
{
k = i;
for (int j = 0; j <= i; j++)
k = k + j;
}

Thank You

k does not loop at all. Its only an integer that sums up the values of j in the last iteration of that i loop. So the final value of k should be 3 + 0 + 1 + 2 + 3 = 9.

code tags really help :)

std::cout << "k: " << k;

or it's really simple to run through that loop in your head... and if you understand what's going on it's even easier to know what the answer is without having to go through the whole loop.

code tags really help :)

Didn't help a bit.

or it's really simple to run through that loop in your head... and if you understand what's going on it's even easier to know what the answer is without having to go through the whole loop.

Agree -- only the final iteration of the i loop is relevant.

Didn't help a bit.

touche ;)

to the OP: indenting your code also makes it easier to follow/read/understand/etc.

For those who suggested the OP:
The OP definitely has been given this problem as a homework by his instructor. The instructor wants OP to be able to tell and understand how looping works in programming. Hence, actually running the program would be a sought of 'cheat'.
I am sure the instructor would have given a note: "Do not use the compiler to solve this problem", but even if he didn't, it would be best to the benefit of the OP, to first understand the predict the answer and then check the answer.

only the third, fourth and fifth iteration of the first loop gives a meaningful change in k.
since the loops run 3 times and for each run we add one to k (k+j) then the final answer is to be 9;
you could easily have run this yourself.

Obviously I could have run this myself in a compiler but that would have just given me the answer. I need to understand how the loop works so when I face this question in a written exam with different values I can work it out. Thank you for your answers but please realise that just because things seem simple to you guys, they are a little confusing to us beginners. So pointless responses like that from Stinomus are very unhelpful.

A "pointless" response might have been to do this:

#include <iostream>

int main()
{
   int k = 0;
   for ( int i = -1; i <= 3; i++ )
   {
      k = i;
      std::cout << "i = " << i << ", k = " << k << "\n";
      for ( int j = 0; j <= i; j++ )
      {
         k = k + j;
         std::cout << " j = " << j << ", k = " << k << "\n";
      }
   }
   return 0;
}

This might have produced the following:

i = -1, k = -1
i = 0, k = 0
 j = 0, k = 0
i = 1, k = 1
 j = 0, k = 1
 j = 1, k = 2
i = 2, k = 2
 j = 0, k = 2
 j = 1, k = 3
 j = 2, k = 5
i = 3, k = 3
 j = 0, k = 3
 j = 1, k = 4
 j = 2, k = 6
 j = 3, k = 9

That particular bit of output would "unhelpful" in discerning a pattern?

[aside]
To each his own. For me it would have been helpful if I were struggling with recognizing the pattern. I probably would have been able to use the code to explain itself to me (and have a response more quickly, if even needing to post a question at all). This sort of debugging -- displaying values during code execution -- is common (for me at least) in all sorts of code. YMMV, I guess.
[/aside]

Obviously I could have run this myself in a compiler but that would have just given me the answer. I need to understand how the loop works so when I face this question in a written exam with different values I can work it out. Thank you for your answers but please realise that just because things seem simple to you guys, they are a little confusing to us beginners. So pointless responses like that from Stinomus are very unhelpful.

'unhelpful' responses usually result from misunderstanding what's being asked.... Anyway, if you were trying to get down what 'for' loops do, this tutorial looked decent:

http://www.hitmill.com/programming/cpp/forLoop.htm

if that's not to your liking I'm sure google has some alternatives.

If you already understand for loops and just want to know how to run through one in your head here's what I do : take it one step at a time and focus on the variable of interest (in this case 'k'). Keep track of what value is being held at 'k' at each step (don't jump around) as you run through your loop (use a piece of paper at first if that's helpful).

if you have a good debugger (I personally have found a liking to VS2008) that could help you see what's going on in a for loop if you set breakpoints throughout it. Doing that for one might clear up on how it actually loops through the block of code (enabling you to do it on your own later)

Hope that's what you were looking for.

~J

This article has been dead for over six months. Start a new discussion instead.