September 2017
Beginner
402 pages
9h 52m
English
Let's consider an example with nested loops:
for 1..5 -> $x { for 1..5 -> $y { say "$x * $y = ", $x * $y; }}This program prints the product table for the numbers from 1 to 5. What if, at some point, we want to skip the rest of the table for the given $x, and continue with the next value of $x? The direct usage of next inside the loop for $y will only affect the inner loop. To make sure that the next statement is modifying the execution of the outer loop, use X_LOOP:
X_LOOP: for 1..5 -> $x { for 1..5 -> $y { next X_LOOP if $y == $x; say "$x $y = ", $x $y; }}
The label here is a capitalized identifier X_LOOP followed by the colon. It is mentioned in the next statement, so the compiler understands that the next iteration should ...
Read now
Unlock full access