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.
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 + " ");
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 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 <= 5; y++)
{ #paired
---
System.out.print("*");
---
}
---
System.out.println();
---
}
---
}
}