Dear ariella,
The point of these exercises is to keep track of the value of each loop's
iterating variable. In both cases, the iterating variable of the outside
loop is i, while the inside loop iterates over j. In order to adhere to
the pattern established by the sample table, we show the value of i only
when it changes. Furthermore, when the iterating variable changes but the
loop does not execute again because its test condition has become false,
we display this in the table as well.
In the first exercise, the inside loop stops executing when j becomes 3,
and the outside loop stops when i becomes 4.
value of i value of j test for i test for j displayed
---------- ---------- ---------- ---------- ---------
0 0 (i <= 3) TRUE (j <= 2) TRUE 0 0
1 (j <= 2) TRUE 0 1
2 (j <= 2) TRUE 0 2
3 (j <= 2) FALSE
1 0 (i <= 3) TRUE (j <= 2) TRUE 1 0
1 (j <= 2) TRUE 1 1
2 (j <= 2) TRUE 1 2
3 (j <= 2) FALSE
2 0 (i <= 3) TRUE (j <= 2) TRUE 2 0
1 (j <= 2) TRUE 2 1
2 (j <= 2) TRUE 2 2
3 (j <= 2) FALSE
3 0 (i <= 3) TRUE (j <= 2) TRUE 3 0
1 (j <= 2) TRUE 3 1
2 (j <= 2) TRUE 3 2
3 (j <= 2) FALSE
4 3 (i <= 3) FALSE
In the second exercise, the inside loop stops executing when j becomes 0,
and the outside loop stops when i becomes 3.
value of i value of j test for i test for j displayed
---------- ---------- ---------- ---------- ---------
0 3 (i < 3) TRUE (j > 0) TRUE 3
2 (j > 0) TRUE 2
1 (j > 0) TRUE 1
0 (j > 0) FALSE
1 3 (i < 3) TRUE (j > 0) TRUE 1 3
2 (j > 0) TRUE 1 2
1 (j > 0) TRUE 1 1
0 (j > 0) FALSE
2 3 (i < 3) TRUE (j > 0) TRUE 2 3
2 (j > 0) TRUE 2 2
1 (j > 0) TRUE 2 1
0 (j > 0) FALSE
3 0 (i < 3) FALSE
It has been a pleasure to address this question on your behalf.
Regards,
leapinglizard |