Humanity

Edit the world by your favorite way

continueブロック

なんとなくメモ。

#!/usr/bin/env perl
use strict;
use warnings;

use Perl6::Say;


say "--- for ---";

# nextしても、$i++は評価される。
# 無限ループになったりはしない。
for (my $i = 0; $i < 10; $i++) {
    next if $i == 5;
    say "foo:$i";
}

say;
say "--- while ---";
# 上のforは次のwhileと等価。
# continueブロックはnextされるかwhile文の最後まで到達した場合に実行されるブロック。

{
    my $i = 0;
    while ($i < 10) {
        next if $i == 5;
        say "foo:$i";
    }
    continue {
        $i++;
    }
}

でもcontinueブロック使うよりはfor使えばいいし、
特定の範囲をイテレートするならforeachの方が安全。