Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we will look at a practical example of using nested for loops to print a rectangle of stars. Who can remind us what a nested loop is?
Isn't it when you have a loop inside another loop?
Exactly! Nested loops allow us to perform multiple iterations within iterations. Now, when we want to print a rectangle, how do you think we would do that?
Maybe we need one loop for each row and another loop for the stars?
Correct! We’ll need an outer loop for rows and an inner loop for the stars. Let's explore the code together.
Here’s the code: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 5; j++) { System.out.print("* "); } System.out.println(); }. Can anyone identify what the outer loop does?
It controls how many times we print the rows, right?
Exactly! The outer loop runs three times, meaning we will have three rows. And what about the inner loop?
It’s responsible for printing the stars. It runs five times for each row.
Great job! So, each complete iteration of the outer loop will print a full row of stars. Let's see what the output would look like.
"After executing the code, we would get:
This example is foundational. Nested loops can be used for much more than printing shapes. Can anyone think of other applications?
Like generating matrices or working with images?
Exactly! They are commonly used in graphics, game development, and data processing tasks. What’s key to remember when using nested loops?
To avoid making the code too complex. We need to keep it readable!
Correct! Always aim for clarity in your code.
For every row, count the stars, the outer controls, the inner's ours.
Imagine a librarian placing five books on each shelf. For three shelves, she uses a special tool called a nested loop to organize.
O.I. for Outer Inner: O controls the rows, I counts the stars.
R.I.P. - Rows Inner Prints: Remember, the rows come first; inner handles stars!
Term
What is an outer loop?
Click to flip
Term
What is an inner loop?
Click to flip
This section provides an example of using nested for loops to print a rectangle of stars in Java.
In this section, we explore how to create a simple pattern using nested for loops by printing a rectangle consisting of stars. The example illustrates the structure and logic of nested loops for generating a specific output.
In this section, we delve into the practical application of nested for loops in Java through a specific example. The goal is to print a rectangle of stars. To achieve this, two nested loops are employed:
The code snippet provided demonstrates this:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print("* ");
}
System.out.println();
}The output generated from this code is three rows of five stars:
* * * * *
* * * * *
* * * * *This simple example lays the foundation for understanding more complex patterns and applications of nested loops in programming.