Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 105 lines (87 sloc) 1.783 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
use v6;

use Test;

# L<S04/Loop statements/last>

=begin description

last
last if <condition>;
<condition> and last;
last <label>;
last in nested loops
last <label> in nested loops

=end description

plan 8;

# test for loops with last

{
    my $tracker = 0;
    for 1 .. 5 {
        $tracker = $_;
        last;
    }
    is($tracker, 1, '... our loop only got to 1 (last)');
}

{
    sub mylast { last; };
    my $tracker = 0;
    for 1 .. 5 {
        $tracker = $_;
        mylast();
    };
    is $tracker, 1, 'can last() outside a subroutine and a for-loop';
}


{
    my $tracker = 0;
    for 1 .. 5 {
        $tracker = $_;
        last if $_ == 3;
    }
    is($tracker, 3, '... our loop only got to 3 (last if <cond>)');
}

{
    my $tracker = 0;
    for 1 .. 5 {
        $tracker = $_;
        $_ == 3 && last;
    }
    is($tracker, 3, '... our loop only got to 3 (<cond> && last)');
}

{
    my $tracker = 0;
    for 1 .. 5 {
        $tracker = $_;
        $_ == 3 and last;
    }
    is($tracker, 3, '... our loop only got to 3 (<cond> and last)');
}

#?pugs skip 'last LABEL'
#?rakudo skip 'last LABEL'
{
    my $var = 0;
    DONE: for (1..2) {
              last DONE;
              $var++;
    };
    is($var, 0, "var is 0 because last before increment")
}

{
    my $tracker = 0;
    for (1 .. 5) -> $out {
        for (10 .. 11) -> $in {
            $tracker = $in + $out;
            last;
        }
    }
    is($tracker, 15, 'our inner loop only runs once per (last inside nested loops)');
}

#?pugs skip 'last LABEL'
#?rakudo skip 'last LABEL'
{
    my $var = 0;
    OUT: for (1..2) {
        IN: for (1..2) {
            last OUT;
        }
        $var++;
    };
    is($var, 0, "var is 0 because last before increment in nested loop");
}

# vim: ft=perl6
Something went wrong with that request. Please try again.