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
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.
i: 0 j: p: 0 s: | | | | | | | | | index 0 1 2 3 4 5 6 7 8 9 screen:
i: 0 1 j: p: 0 s: | | | | | | | | | index 0 1 2 3 4 5 6 7 8 9 screen:
i: 0 1 j: p: 0 s: 97| | | | | | | | | index 0 1 2 3 4 5 6 7 8 9 screen:
i: 0 1 j: p: 0 1 s: 97| | | | | | | | | index 0 1 2 3 4 5 6 7 8 9 screen:
i: 0 1 j: 0 p: 0 1 s: 97| | | | | | | | | index 0 1 2 3 4 5 6 7 8 9 screen:
i: 0 1 j: 0 1 p: 0 1 s: 97| | | | | | | | | index 0 1 2 3 4 5 6 7 8 9 screen:
i: 0 1 j: 0 1 p: 0 1 s: 97| 98| | | | | | | | index 0 1 2 3 4 5 6 7 8 9 screen:
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.
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.
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.
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.
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.
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.
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