Search⌘ K
AI Features

Looping Directives: The C-style for Loop

Explore the C-style for loop in Perl to understand its syntax, initialization, condition checking, and iteration subexpressions. Learn how to control loop scope and iteration effectively while recognizing when to choose the foreach loop for more idiomatic Perl code.

Syntax of C-style for loop

The C-style for loop requires us to manage the conditions of iteration:

Perl
for (my $i = 0; $i <= 10; $i += 2) {
say "$i * $i = ", $i * $i;
}

We must explicitly assign to an iteration variable in the looping construct, since this ...