Skip to content

Commit e8c626b

Browse files
authored
Merge pull request #3134 from threadless-screw/control-flow-loop
Fixes: #3107
2 parents acab715 + ab50c3e commit e8c626b

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

doc/Language/control.pod6

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -890,18 +890,25 @@ C<given> can follow a statement to set the topic in the statement it follows.
890890
=head1 X<loop|control flow, loop>
891891
892892
The C<loop> statement takes three statements in parentheses separated by C<;>
893-
that take the role of initializer, conditional and incrementer. The initializer
894-
is executed once and any variable declaration will spill into the surrounding
895-
block. The conditional is executed once per iteration and coerced to C<Bool>,
896-
if C<False> the loop is stopped. The incrementer is executed once per
897-
iteration. CAUTION: Even though the loop iterator variable is I<inside> the loop's
898-
parentheses, it is a lexical variable in the loop's I<outer or containing> scope and can
899-
be used in code following the loop.
900-
901-
loop (my $i = 0; $i < 10; $i++) {
893+
that take the roles of initializer, conditional and incrementer, respectively.
894+
The initializer is executed once before the conditional is first tested. In case
895+
the initializer involves a variable declaration, the variable is declared as a
896+
lexical variable in the loop's I<outer or containing> scope so that it can be
897+
used in code following the loop statement. The conditional is executed before
898+
each iteration and coerced to C<Bool>; if C<False> the loop is stopped. The
899+
incrementer is executed after each iteration, and before the conditional is
900+
tested again.
901+
902+
loop (my $i = 0; $i < 10; $i++) { # A typical loop
902903
say $i;
903904
}
904905
906+
my @str = "However Long".comb; # Our very own .char routine:
907+
loop (my $l = 0;;) { # Declare $l in outer scope
908+
last if !@str[$l++] # and count chars until we hit an
909+
} # undefined element (Any)
910+
say "The string is {--$l} chars long.";
911+
905912
The infinite loop does not require parentheses.
906913
907914
=for code

0 commit comments

Comments
 (0)