4.4. Nested For Loops

A nested loop has one loop inside of another. These are typically used for working with two dimensions such as printing stars in rows and columns as shown below. When a loop is nested inside another loop, the inner loop is runs many times inside the outer loop. In each iteration of the outer loop, the inner loop will be re-started. The inner loop must finish all of its iterations before the outer loop can continue to its next iteration.


Figure 1: Nested Loops

coding exercise Coding Exercises

What does the following code print out? Watch the code run in the Java visualizer by clicking the CodeLens button and then forward. Notice how the inner loop is started over for each row. Can you predict how many rows and columns of stars there will be?

Can you change the code to be a 10x8 rectangle? Try replacing line 10 with this print statement to see the rows and columns: System.out.print(row + “-” + col + ” “);

exercise Check your understanding

The main method in the following class should print 10 rows with 5 <code>*</code> in each row. But, the blocks have been mixed up and include <b>one extra block</b> that isn’t needed in the solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the <i>Check Me</i> button to check your solution.</p>

coding exercise Coding Exercise

Try a nested loop with turtles! If the code below does not work in your browser, you can copy the code into this repl.it link (refresh page after forking and if it gets stuck) or download the files here to use in your own IDE.

The turtle below is trying to draw a square many times to create a snowflake pattern. Can you change the outer loop so that the pattern completes all the way around? Try different ending values for the counter i to find the smallest number that works between 5 and 15.

4.4.1. groupwork Programming Challenge : Turtle Snowflakes

In the last exercise, you used nested for-loops to have the turtle draw a square repeatedly to make a snowflake. Use the Active Code window below or this repl.it link to have yertle draw the following shapes using nested loops.

  1. Complete the code in the active code window below to draw a snowflake of triangles. How many times did you need to run the outer loop to go all the way around?

  2. In the exercise above, you figured out how many times to run the outer loop to finish the snowflake. You may have noticed that the number of times the loop needs to run is related to the angle you turn before drawing the next triangle (30 degrees). These turns have to add up to 360 degrees to go all the way around. Create a variable called turnAmount to use instead of 30 in the last turn command and change it to a different value (try 45 or 15). Change the outer loop so that it runs the number of times needed by using a formula with this turnAmount variable and 360. Can you draw a snowflake using more or less triangles than before?

  3. Create another variable called n for the number of sides in the polygon the inner loop draws. Change the angle in the inner loop to also use a formula with 360 and this new variable. Can you change your snowflake to draw squares or pentagons instead? (Note this may overwhelm the Active Code server, so you may need to switch to using this repl.it link or your own IDE).

  4. Let’s add some more color! Add an if/else statement that changes the Color of the pen before the inner loop depending on whether the outer loop variable is odd or even. Remember that even numbers have no remainder when divided by 2.

  5. Be creative and design a unique snowflake!

Use nested for-loops to have the turtle draw a snowflake of polygons. Use the variable turnAmount to turn after each shape and the variable n for the sides of the polygon.

4.4.2. Summary

  • Nested iteration statements are iteration statements that appear in the body of another iteration statement.

  • When a loop is nested inside another loop, the inner loop must complete all its iterations before the outer loop can continue.

You have attempted of activities on this page