to problem set

Problem 9 - Code Comprehension

int sum = 0;
for (int i = 0; i < 4; i++) {
    for (int j = i; j < i * 2; j++) {
        if (j < 4) {
            sum++;
        } else {
            sum--;
        }
    }
}

I recommend splitting your screen so you can follow along with the explanation as you look at the above code.

Start of outer for loop: i (0)
Should the inner loop run? Is i (0) less than i * 2?
No, don't run inner for loop.
_________________________
_________________________
Checking to see if outer loop should run again:
Is i (1) less than 4?
_________________________
Start of outer for loop: i (1)
Should the inner loop run? Is i (1) less than i * 2?
Yes, run inner for loop.
_________________________
_________________________
Start of inner for loop: i (1) and j (1)
_________________________
Is j (1) less than 4?
Yes, increase sum (0) by 1, sum = 1
Checking to see if inner loop should run again (increment j by 1):
Is j (2) less than (i (1) * 2) = 2?
No, break out of this for loop back to the outer loop.
_________________________
Checking to see if outer loop should run again:
Is i (2) less than 4?
_________________________
Start of outer for loop: i (2)
Should the inner loop run? Is i (2) less than i * 2?
Yes, run inner for loop.
_________________________
_________________________
Start of inner for loop: i (2) and j (2)
_________________________
Is j (2) less than 4?
Yes, increase sum (1) by 1, sum = 2
Checking to see if inner loop should run again (increment j by 1):
Is j (3) less than (i (2) * 2) = 4?
Yes, run again, increase j (2) by 1, j = 3
_________________________
Start of inner for loop: i (2) and j (3)
_________________________
Is j (3) less than 4?
Yes, increase sum (2) by 1, sum = 3
Checking to see if inner loop should run again (increment j by 1):
Is j (4) less than (i (2) * 2) = 4?
No, break out of this for loop back to the outer loop.
_________________________
Checking to see if outer loop should run again:
Is i (3) less than 4?
_________________________
Start of outer for loop: i (3)
Should the inner loop run? Is i (3) less than i * 2?
Yes, run inner for loop.
_________________________
_________________________
Start of inner for loop: i (3) and j (3)
_________________________
Is j (3) less than 4?
Yes, increase sum (3) by 1, sum = 4
Checking to see if inner loop should run again (increment j by 1):
Is j (4) less than (i (3) * 2) = 6?
Yes, run again, increase j (3) by 1, j = 4
_________________________
Start of inner for loop: i (3) and j (4)
_________________________
Is j (4) less than 4?
No, decrease sum (4) by 1, sum = 3
Checking to see if inner loop should run again (increment j by 1):
Is j (5) less than (i (3) * 2) = 6?
Yes, run again, increase j (4) by 1, j = 5
_________________________
Start of inner for loop: i (3) and j (5)
_________________________
Is j (5) less than 4?
No, decrease sum (3) by 1, sum = 2
Checking to see if inner loop should run again (increment j by 1):
Is j (6) less than (i (3) * 2) = 6?
No, break out of this for loop back to the outer loop.
_________________________
Checking to see if outer loop should run again:
Is i (4) less than 4?


sum = 2