Lesson 7 of 15
For / Foreach
For Loops
Perl's for loop can iterate over a range:
for my $i (1..5) {
say $i;
}
The 1..5 range operator produces the list (1, 2, 3, 4, 5).
Foreach
foreach iterates over a list or array:
my @colors = ("red", "green", "blue");
foreach my $color (@colors) {
say $color;
}
In Perl, for and foreach are interchangeable:
for my $color (@colors) {
say $color;
}
Your Task
Print the squares of numbers 1 through 5 (1, 4, 9, 16, 25), each on its own line.
JS Transpiler loading...
Loading...
Click "Run" to execute your code.