Iterative Loops: Nested Loops

    Iterative Loops: Nested Loops

      Loops and Morpheus have a surprising amount in common. They can both

      • make code look sleek and cool.
      • control exactly what happens in the code.
      • help us get through the Matrix.

      Hmm...maybe not just the Matrix, but a for loop can help code make it through any mathematical matrix. Outside of Hollywood, a matrix is a grid of numbers or variables, which can be thought of as a list of lists. Loops can get through lists of lists by nesting one inside the other. The outer loop will iterate through the list of rows and the inner loop will iterate through that row's elements (the columns in that row).

      Nesting For Loops

      Check it: here comes an example. Say we have a two-dimensional list with six columns and five rows, filled with numbers from 1 – 30.

      123456
      789101112
      131415161718
      192021222324
      252627282930

      Here's the pseudocode for one loop, which would equal a single row in the two-dimensional list.

      zeList = {1, 2, 3, 4, 5, 6}
      for element IN zeList:
      	PRINT element
      END FOR

      All that will print out the following row.

      1 2 3 4 5 6

      That's one level done. Now we need a for loop to iterate through each row of elements. To do this, we'll wrap this larger loop around the inner loop, which handles the elements in a single row.

      zeMatrix ={
      			 {1, 2, 3, 4, 5, 6},
      			 {7, 8, 9, 10, 11, 12},
      			 {13, 14, 15, 16, 17, 18},
      			 {19, 20, 21, 22, 23, 24},
      			 {25, 26, 27, 28, 29, 30}
      }
        
      FOR row IN zeMatrix:
      	FOR element IN row:
      		PRINT element
      	END FOR
      	PRINT new line
      END FOR

      This set-up of loops will print out the following text.

      1 2 3 4 5 6 
      7 8 9 10 11 12 
      13 14 15 16 17 18 
      19 20 21 22 23 24 
      25 26 27 28 29 30

      Nesting While Loops

      While loops can also be nested inside each other, but this sort of nesting isn't usually as helpful as nesting for loops. In fact, most of the times that you could put one while loop inside of another, you could get the same result with one while loop and a two-part condition.

      For example, Shmoop's refrigerator has been having issues…refrigerating. To fix the problem, we made an app that tells us whenever the temperature of the fridge goes above 45° Fahrenheit or below 32° Fahrenheit. It's a big program, but the nested loop controlling everything looks something like this.

      WHILE temp < 45:
      	WHILE temp > 32:
      		check(temp)
      		IF temp > 45:
      			BREAK
      		END IF
      	END WHILE
      	alertShmoopOfTempChange()
      END WHILE

      It might get the job done, but boy does it look ugly, what with checking temp against 45 each time. There's got to be a better way, and…there is.

      WHILE ((temp > 32) AND (temp < 45)):
      	check(temp)
      END WHILE
      alertShmoopOfTempChange()

      With a little help from our friend, the logical and operator, we can make sure both conditions are true throughout the loop, significantly cutting down on the lines of code needed to keep the loop alive.

      Bye-bye freezer burn.

      We should warn you: there isn't really a limit to the amount of loops you can wrap around each other. That might not sound like a bad thing, but with enough wrapping the code gets less and less efficient to the point where it might not ever stop running.

      Good for refrigerators, bad for computer programs.