Playing Computer: Nested Loops

Consider the program below. As usual we have numbered the statements so we and refer to them by number.

0.   char s[200];
1.   int p =0;
2.   int i=0;
3.   while (i< 2) {
4.      i++;
5.      s[p] = 'a';
6.      p++;
7.      int j = 0;
8.      while (j<3) {
9.         j++;
10.        s[p] = 'b';
11.        p++;
12.     }
13.  }
14.  s[p]=0;
15.  p++;
16.  printf(s)
Important: Lines 8-12 make up a loop that is nested inside the outer loop that runs from line 3 to line 13. We call lines 8-12 the inner loop. C treats the inner loop as a unit. The execution of this unit or statement is not done until j is no longer less than 3. The inner loop is the last statement in the outside loop. So when the inner loop is done, execution returns to line 3. If i is still less than 2, then we do ALL the statements in the body of the outside loop again. this includes the inside loop. If i is no longer less than 2, the outer loop is finished and we go on to whatever comes after it,-- lines 14-16.

Playing Computer

  1. Before execution our piece of paper looks like:
        i:
        j:
        p:
        s:   |   |   |   |   |   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
    where the separate cells of the array are separated by the | symbol. I can not show you all the array so I settle for the first 10 cells.
  2. Lines 0-2 execute:
        i: 0
        j:
        p: 0
        s:   |   |   |   |   |   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
  3. Statement 3 executes; since i is less than 2 we will be doing all of the statements in the compound statement that starts in 3 (open brace) and continues to line 13 (close brace). We've just made the decision to do these statements; So there is no change in the variables.
  4. Line 4 executes; The new value in i is 1 we write it to the right of the old value.
        i: 0 1
        j:
        p: 0
        s:   |   |   |   |   |   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
  5. Statement 5 executes; 'a' is put into s[p]; Since p has a zero in it, we put it into the array at index 0.
        i: 0 1
        j:
        p: 0
        s: 97|   |   |   |   |   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
  6. Statement 6 executes. Purpose: the next time we use p as an index (str[p] = ??) when we are putting something into the array, the new item will put right after the 'a'.
        i: 0 1
        j:
        p: 0 1
        s: 97|   |   |   |   |   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
  7. Statement 7 executes.
        i: 0 1
        j: 0
        p: 0 1
        s: 97|   |   |   |   |   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
  8. Statement 8 executes. Since the value in j is less than 3 we will do the compound statement coming after the while. No changes in the variables yet.
  9. Statement 9 executes. Now j is 1 larger.
        i: 0 1
        j: 0 1
        p: 0 1
        s: 97|   |   |   |   |   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
  10. Statement 10 executes. Since p has a 1 in it, 'b' is put into the second position in the array.
        i: 0 1
        j: 0 1
        p: 0 1
        s: 97| 98|   |   |   |   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
  11. Line 11 executes.
        i: 0 1
        j: 0 1
        p: 0 1 2
        s: 97| 98|   |   |   |   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
    That is the last statement in the body of the inside loop. We go back to the header of the inside loop, line 8. We check the condition in line 8. Variable j is still less than 3. So we will get a second rep of the body, the compound statement.
  12. So We will do lines 9, 10, and 11:
        i: 0 1
        j: 0 1 2
        p: 0 1 2 3
        s: 97| 98| 98|   |   |   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
    Line 11 is the last statement in compound statement that is the body of the inside loop. So we go back to check the condition in the header of the inside loop. Variable j is still less than 3. So we will get another rep of the statements in the body.
  13. So We will do lines 9, 10, and 11:
        i: 0 1
        j: 0 1 2 3
        p: 0 1 2 3 4
        s: 97| 98| 98| 98|   |   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
    Line 11 is the last statement in compound statement that is the body of the inside loop. So we go back to check the condition in the header of the inside loop. Variable j is NOT less than 3. We are through with this execution of the inside loop. Since the inside loop (lines 8-12) is the last statement of the outside we go back to the header of the outside loop, line 3. We check variable i is less than 2. So we will get another rep of the statements in body of the outside loop.
  14. We do lines 4-7:
        i: 0 1 2
        j: 0 1 2 3 0
        p: 0 1 2 3 4 5
        s: 97| 98| 98| 98| 97|   |   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
    What comes next is the execution of statement running from line 8-12. What we are calling the inside loop. We check the condition after the while in line 8. Variable j (just made 0 in line 7) is less than 3. So we will execute the body of the inside loop.
  15. We do lines 9, 10, 11.
        i: 0 1 2
        j: 0 1 2 3 0 1
        p: 0 1 2 3 4 5 6
        s: 97| 98| 98| 98| 97| 98|   |   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
    Then we go back and check the condition in the inner loop header. Variable j is still less than 3.
  16. So We do another rep of lines 9, 10, 11:
        i: 0 1 2
        j: 0 1 2 3 0 1 2
        p: 0 1 2 3 4 5 6 7
        s: 97| 98| 98| 98| 97| 98| 98|   |   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
    Then we go back and check the condition in the inner loop header. Variable j is still less than 3.
  17. So We do another rep of lines 9, 10, 11:
        i: 0 1 2
        j: 0 1 2 3 0 1 2 3
        p: 0 1 2 3 4 5 6 7 8
        s: 97| 98| 98| 98| 97| 98| 98| 98|   |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    
    Then we go back and check the condition in the inner loop header. Variable j is NOT less than 3. So execution of the inside loop is finished. Since it is the last statement of the outside loop we go back it the header of the outside loop in line 3. Variable i is NOT less than 2. So the outside loop is finished. There are statements coming after the outside loop which ends in line 13. So we do lines 14, 15, and 16.
        i: 0 1 2
        j: 0 1 2 3 0 1 2 3
        p: 0 1 2 3 4 5 6 7 8 9
        s: 97| 98| 98| 98| 97| 98| 98| 98| 0 |
    index  0   1   2   3   4   5   6   7   8   9
    screen:
    abbbabbb
    
Homework H3.2