Solution Review: Printing Pyramid
Explore Perl loop structures by reviewing a solution that prints a pyramid pattern. Learn how nested for loops work in Perl, including how the outer and inner loops control the number of printed characters and lines. This lesson helps you solidify your understanding of loops for practical text output tasks.
We'll cover the following...
We'll cover the following...
Let’s look at the solution first before jumping into the explanation:
Explanation
The outer for loop executes $rows times, and the inner for loop executes once for the first iteration of outer loop, twice for the second iteration of the outer loop, and so on. The inner loop prints an a character on the console every time it executes. At the end of the inner loop, it prints a \n to get us on a new line.