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 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.
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 print a rectangle with 10 rows and 8 columns of stars? You can also try replacing line 10 with this print statement to see the rows and columns: System.out.print(row + "-" + col + " ");
Check your understanding
4-4-2: What does the following code print?
for (int i = 1; i 7; i++) for (int y = 1; y 5; y++) System.out.print("*"); > System.out.println(); >
4-4-3: What does the following code print?
for (int i = 0; i 5; i++) for (int j = 3; j >= 1; j--) System.out.print("*"); > System.out.println(); >
The main method in the following class should print 10 rows with 5 *s in each row. But, the blocks have been mixed up and include one extra block 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 Check button to check your solution.
public class Test1 < public static void main(String[] args) < --- for (int x = 0; x < 10; x++) < --- for (int y = 0; y < 5; y++) < --- for (int y = 0; y --- System.out.println(); --- > --- > >
Try nested loops with turtles to create a snowflake design!
Coding Exercise
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.
To make the drawing faster, you can call the World or Turtle object’s setSpeed method with a 0-100 delay value where 0 is the fastest. If the code below does not work in your browser, you can copy the code into this replit link (refresh page after forking and if it gets stuck) or download the files here to use in your own IDE.
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 replit link to have yertle draw the following shapes using nested loops. We encourage you to work in pairs on this.
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.
To make the drawing faster, you can call the World or Turtle object’s setSpeed method with a 0-100 delay value where 0 is the fastest. If the code below does not work in your browser, you can copy the code into this replit link (refresh page after forking and if it gets stuck) or download the files here to use in your own IDE.