Course of Raku / Advanced / Control flow / Block-related phasers / Exercises / After each step
Solution: After each step
Here is a possible solution to the task.
Code
my $sum = 0;
for 1..3 {
NEXT say "sum so far: $sum";
$sum += $_;
}🦋 You can find the source code in the file next-phaser.raku.
Output
sum so far: 1
sum so far: 3
sum so far: 6Comments
The
NEXTphaser is written first, but it runs at the end of each iteration — after the body has added the current number to$sum.So after the first pass
$sumis1, after the second it is3, and after the third it is6.NEXTis the loop counterpart of running something between iterations, distinct fromFIRST(once at the start) andLAST(once at the end).
Course navigation
← After each
step | Quiz —
LEAVE →