September 2017
Beginner
402 pages
9h 52m
English
The loop keyword expects three elements to control the number or repetitions of the loop body. Consider the following code snippet:
loop (my $c = 0; $c < 5; $c++) { say $c;}
In this example, $c is the counter of the loop iterations. This variable is declared and initialized immediately after the loop keyword—my $c = 0. The body of the loop is executed if the condition $c < 5 is True. After the iteration, the $c++ statement is executed, which increments the counter, and the cycle repeats. As soon as $c becomes equal to five, the condition is not True anymore, and the loop stops. So, the whole program prints the numbers from 0 up to, and including, 4.
Some, or even all of the parts in the loop header may be omitted. For example, ...
Read now
Unlock full access