The table below shows an example of code tracing with a loop.
The fragment of program find the sum of n terms of a geometric progression
(Lab 5). I chose the value n=4, which is big enough to show
several repetitions and small enough not to bore too much.
double b,q; /* initial term and quotient of geom.progression */ double s; /* sum-to-be (accumulator) */ int n; /* number of terms to be summed */ double qi; /* accumulator for powers q^i */ int i; /* counter of iterations in loop */ 10 b=3; /* initializing b */ 11 q=2; /* ... and q */ 12 n=4; /* ... and number of terms */ 13 s=b; /* initializing sum */ 14 qi=1; /* initializing power: qi=q^0=1 */ 15 for (i=1; i<n; i++) 16 { 17 qi=qi*q; /* updating power: qi was q^(i-1), becomes q^i */ 18 s= s+ b*qi; /* updating sum */ 19 printf("i=%d, q^i=%lf, sum=%lf\n", i,qi,s); 20 } 21 printf("After loop: i=%d, q^i=%lf, sum=%lf\n", i,qi,s);
Please note the exact sequence of loop elements: the initialization section (i=1) is executed only once, condition check precedes the body. After the condition fails, program skips the loop body and proceeds to the line immediately after the closing brace.
line # of| b | q | n | s | i | qi | Comments ------------------------------------------------- 10-12 | 3 | 2 | 4 | ? | ? | ? | ------------------------------------------------- 13-14 | 3 | 2 | 4 | 3 | ? | 1 | values immediately before loop ------------------------------------------------- 15 | " | " | " | " | 1 | " | initialization of loop: i=1 ------------------------------------------------- 15 | Checking loop condition i<4? => True => to loop body ---------------------------------------------------- 17 | 3 | 2 | 4 | 3 | 1 | 2 | qi=1*2=2 ---------------------------------------------------- 18 | 3 | 2 | 4 | 9 | 1 | 2 | s=3+3*2=9 ----------------------------------------------------- 19 | Printing: i=1, q^i=2, sum=9 ------------------------------------------------------ 20 | | back to loop header (line 15) -------------------------------------------------------- 15 | 3 | 2 | 4 | 9 | 2 | 2 | loop update (i++) -------------------------------------------------------- 15 | Checking loop condition i<4? => True ------------------------------------------------------- 17 | 3 | 2 | 4 | 9 | 2 | 4 | qi=2*2=4 ------------------------------------------------- 18 | 3 | 2 | 4 | 21 | 2 | 4 | s=9+3*4=21 ------------------------------------------------- 19 | Printing: i=2, q^i=4, sum=21 ------------------------------------------------- 20 | | back to loop header (line 15) -------------------------------------------------------- 15 | 3 | 2 | 4 | 21 | 3 | 4 | loop update (i++) ------------------------------------------------------- 15 | Checking loop condition i<4? => True -------------------------------------------------------- 17 | 3 | 2 | 4 | 21 | 3 | 8 | qi=4*2=8 ------------------------------------------------- 18 | 3 | 2 | 4 | 45 | 3 | 8 | 21=3+3*8=45 ------------------------------------------------- 19 | Printing: i=3, q^i=8, sum=45 ------------------------------------------------- 20 | | back to loop header (line 15) -------------------------------------------------------- 15 | 3 | 2 | 4 | 45 | 4 | 8 | loop update (i++) ------------------------------------------------- 15 | Checking loop condition i<4? => False => exit loop -------------------------------------------------- 21 | Printing: i=4, q^i=8, sum=45
Note that the last printed values of i and q^i are inconsistent. This is because i has been incremented one more time, before the computer detected that the loop condition finally failed.