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: 6

Comments

  1. The NEXT phaser is written first, but it runs at the end of each iteration — after the body has added the current number to $sum.

  2. So after the first pass $sum is 1, after the second it is 3, and after the third it is 6. NEXT is the loop counterpart of running something between iterations, distinct from FIRST (once at the start) and LAST (once at the end).

Course navigation

After each step   |   Quiz — LEAVE