to problem set

Problem 7 - Code Comprehension

The code that is being run:

int j=0;
int k=0;
for (int i=1; i<10; i += j) {
    j = 5;
    while (j > i) {
        k += j;
        j--;
    }
}

Flow of the program (probably helps to open the above code in another window so you can follow along).

start of the program: 
j = 0
k = 0
_________________________
_________________________
Start of the for loop: 
i = 1
j = 0
k = 0
_________________________
After j = 5; Now checking while loop
j (5) is currently greater than i (1) So we are going to go into the while loop.
Inside while loop: 
j = 5 and i = 1
increasing k (0) by j (5), k = 5
Now we are decreasing j from 5 to 4
End of the while loop, recheck the j > i
j (4) is currently greater than i (1) So we are going to run the loop again.
Inside while loop: 
j = 4 and i = 1
increasing k (5) by j (4), k = 9
Now we are decreasing j from 4 to 3
End of the while loop, recheck the j > i
j (3) is currently greater than i (1) So we are going to run the loop again.
Inside while loop: 
j = 3 and i = 1
increasing k (9) by j (3), k = 12
Now we are decreasing j from 3 to 2
End of the while loop, recheck the j > i
j (2) is currently greater than i (1) So we are going to run the loop again.
Inside while loop: 
j = 2 and i = 1
increasing k (12) by j (2), k = 14
Now we are decreasing j from 2 to 1
End of the while loop, recheck the j > i
j (1) is currently less than or equal to i (1) So we are going to break out the loop.
_________________________
Start of the for loop: 
i = 2
j = 1
k = 14
_________________________
After j = 5; Now checking while loop
j (5) is currently greater than i (2) So we are going to go into the while loop.
Inside while loop: 
j = 5 and i = 2
increasing k (14) by j (5), k = 19
Now we are decreasing j from 5 to 4
End of the while loop, recheck the j > i
j (4) is currently greater than i (2) So we are going to run the loop again.
Inside while loop: 
j = 4 and i = 2
increasing k (19) by j (4), k = 23
Now we are decreasing j from 4 to 3
End of the while loop, recheck the j > i
j (3) is currently greater than i (2) So we are going to run the loop again.
Inside while loop: 
j = 3 and i = 2
increasing k (23) by j (3), k = 26
Now we are decreasing j from 3 to 2
End of the while loop, recheck the j > i
j (2) is currently less than or equal to i (2) So we are going to break out the loop.
_________________________
Start of the for loop: 
i = 4
j = 2
k = 26
_________________________
After j = 5; Now checking while loop
j (5) is currently greater than i (4) So we are going to go into the while loop.
Inside while loop: 
j = 5 and i = 4
increasing k (26) by j (5), k = 31
Now we are decreasing j from 5 to 4
End of the while loop, recheck the j > i
j (4) is currently less than or equal to i (4) So we are going to break out the loop.
_________________________
Start of the for loop: 
i = 8
j = 4
k = 31
_________________________
After j = 5; Now checking while loop
j (5) is currently less than or equal to i (8) So we are going to skip the while loop.
Final value for j: 5
Final value for k: 31