Monday 2 December 2013

#FLMobiGame - Nested Loops in Programming

Nested constructs in programming are a powerful tool for a programmer. However, understanding them in the first instance is not the easiest thing. Here I am trying to explain a specific question that we have used in FLMobiGame which participants found difficult.

Line 1: int myInt = 0; 
Line 2: for (int index1 = 0; index1 < 3 ; index1++) { 
Line 3:   for (int index2 = 0; index2 < 3 ; index2++){ 
Line 4:       myInt = myInt +1; 
Line 5:       if (myInt == 7) 
Line 6:            myInt = 0; 
Line 7:     
Line 8: }

This example is written in Java and there are two loops one inside the other (nested loops). If condition is also within the inner loop.

Let us look at the execution of this programme now.
If the code was
Line 1: int myInt = 0; 
Line 2: for (int index1 = 0; index1 < 3 ; index1++) { 
Line 3:   
Line 4:       myInt = myInt +1; 
Line 5:      
Line 6:          
Line 7:      
Line 8: }

It would have been easier to understand.
This is the familiar one for loop that increments a counter inside it. So at the start index1 = 0 and until index1 < 3 it loops and increments myInt by one each time the loop is run. Here the loop will run when index = 0, index = 1, and index = 2; that is three times incrementing myInt to 3. That was easy. Now let us look at what happens when the second loop comes along.

Line 1: int myInt = 0; 
Line 2: for (int index1 = 0; index1 < 3 ; index1++) { 
Line 3:   for (int index2 = 0; index2 < 3 ; index2++){ 
Line 4:       myInt = myInt +1; 
Line 5:      
Line 6:          
Line 7:     
Line 8: }

Here inside the first for loop with index1 there is another for loop with index2.
Let us look at how this execute.
At the beginning in Line1 myInt is assigned a zero.
At Line 2 execution of first For loop commence. Now index1 is equal to zero and the condition index1 <3 is satisfied. So it goes into the for loop to execute the loop body or the block of code within this for loop.
When loop body begins execution there is another for loop (Line 3). So now the execution starts working on this inner for loop with index2.
At this point if we look at the problem in a different angle we are again faced with the familiar single loop

Line 3:   for (int index2 = 0; index2 < 3 ; index2++){ 
Line 4:       myInt = myInt +1; 
Line 5:      
Line 6:          
Line 7:     }

index2 starts with zero and as long as index2 is less than 3 the loop executes by incrementing myInt.
When the loop ends myInt will be incremented to 3 because this inner loop runs 3 times.
When we come to Line 7 after finishing the execution of the loop we again have to look at the bigger picture.

 Line 1: int myInt = 0; 
Line 2: for (int index1 = 0; index1 < 3 ; index1++) { 
Line 3:   for (int index2 = 0; index2 < 3 ; index2++){ 
Line 4:       myInt = myInt +1; 
Line 5:      
Line 6:          
Line 7:     
Line 8: }

When we get to Line 8 because we know it is the finish of first iteration of index1 for loop we need to go back to index1 loop again to check the condition. 
In this iteration index1 = 1 and index1<3 so we do the whole thing again. That is we go into the body of the index1 for loop and execute it. In this body we find the index2 loop and once again we execute this loop three times. Now myInt is 6 and we have finished index2 for loop execution for the second time round.
We go back to Line 8.
This shows that we are in the block of index1 for loop so we come back to Line 2 and now index1 is equal to two.
index1 is still < 3 so we go into the body of the for loop and find the index2 for loop.
We execute index2 for loop again in full and reach Line 8 for the third time. Now myInt has a value of 9.
This time round index1 is equal to 3 and then the condition index1<3 fails and the execution of the for loop finishes.
At this point we  have myInt with value 9.

I hope that the execution of nested for loops is now clear. 

But wait, our program has an if condition within the index2 for loop. We ignored it this whole time. Now let's put that in place too.

Line 1: int myInt = 0; 
Line 2: for (int index1 = 0; index1 < 3 ; index1++) { 
Line 3:   for (int index2 = 0; index2 < 3 ; index2++){ 
Line 4:       myInt = myInt +1; 
Line 5:       if (myInt == 7) 
Line 6:            myInt = 0; 
Line 7:     
Line 8: }

Both index1=0 and index1=1 iterations of the outer for loop myInt value does not get incremented to 7. Because at the end of second iteration of the index1 for loop myInt was equal to 6. 
In the next iteration - that is index1 = 2 iteration myInt gets incremented to seven.
Let's consider this iteration more closely.

We reach Line3 in the third iteration of index1 loop with index1 = 2 and myInt having a value of 6.
Line 3 for loop begins to execute.
When the index2 loop begins execution with index2=0 inside the body of index2 loop myInt gets incremented to 7 (Line 4). Now in Line 5 the 'if' condition is satisfied and myInt gets a value zero assigned to it.
Next iteration of index2 loop in this run increments myInt to 1 and third iteration increments myInt to 2.

index1 = 0
    index2 = 0; myInt = 1
    index2 = 1; myInt = 2
    index2 = 2; myInt = 3
index1 =1
    index2 = 0; myInt = 4
    index2 = 1; myInt = 5
    index2 = 2; myInt = 6
index1 = 2
    index2 = 0; myInt = 7 --> goes into if condition and gets reset to zero --> myInt = 0
    index2 = 1; myInt = 1
    index2 = 2; myInt = 2
So at the end of this program myInt has a value of 2.

No comments:

Post a Comment