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're going to explore nested loops, specifically how they help us print patterns like a right-angled triangle. Can anyone remind me what a nested loop is?
A nested loop is a loop inside another loop!
Exactly, great job! So, in our example, we will use nested for loops to create a pattern of stars. The outer loop will decide how many lines we have, while the inner loop controls how many stars are printed per line. Remember: O.I. - Outer Iterates.
O.I. is a good way to remember that the outer loop is the one that runs through its iterations first!
Exactly! Now, let’s look at the code to see how that plays out.
Here’s our code: for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print('* '); } System.out.println(); } Can anyone break this down?
The outer loop runs from 1 to 5, so it will iterate 5 times!
Correct! And what about the inner loop?
The inner loop runs from 1 to the current number of the outer loop, which means it prints more stars in each line!
Exactly right! So, in the first iteration of the outer loop, it prints one star, then two in the next, and so forth. This creates our triangle.
Now let’s look at the output of our program. Can anyone tell me what the pattern will look like?
It should look like a triangle made up of stars, increasing each row!
"Exactly! So the output will look like this:
Loops inside loops, up high, count the stars as they fly!
Imagine climbing a staircase where each step up you see more stars light up the night sky, one, two, three, and so on until you reach five!
I.S.O. - Inner loop Stacks Over outer loop for a triangle!
ITERATE - Inner Triangulation Effective Recursion And Total Elements.
Term
What is a nested loop?
Click to flip
Term
What does the outer loop control?
Click to flip
Term
What does the inner loop do?
Click to flip
A loop that exists inside another loop.
The first loop in a nested loop structure that controls the number of iterations for the entire set of loops.
The loop that executes inside the outer loop and runs its complete iteration for each iteration of the outer loop.
A single cycle through a loop.
This section illustrates how to use nested for loops to print a right-angled triangle pattern using stars.
The section provides a code example of how nested for loops operate, specifically focusing on printing a right-angled triangle of stars. It emphasizes how the inner loop manages the number of stars printed in each row, corresponding to the outer loop's iteration.
This section demonstrates the use of nested for loops to print a right-angled triangle pattern of stars in Java programming. The key code snippet given involves two loops: an outer loop that runs from 1 to 5 and an inner loop that runs from 1 to the current iteration of the outer loop. Each iteration of the outer loop corresponds to a new line of output, which increases the number of stars printed in each subsequent line. This illustrates how to manipulate nested loops effectively to create geometric patterns, which is an essential skill in programming for generating output formats.