On Sat, Jun 17, 2017 at 4:19 AM, ToddAndMargo <toddandma...@zoho.com> wrote:
> On 06/16/2017 06:08 PM, Brandon Allbery wrote:
>>
>> On Fri, Jun 16, 2017 at 9:02 PM, ToddAndMargo <toddandma...@zoho.com
>> <mailto:toddandma...@zoho.com>> wrote:
>>
>>     I am afraid I am not understanding "next" again.
>>
>>     When you invoke it, does it pop you back at the start of
>>     the loop or just cough up the next value?
>>
>>
>> The former. Or you can think of it as jumping past everything else in the
>> loop body so the next thing it does is get the next value and run the loop
>> body with it.
>
>
> Hi Brandon,
>
> I kept thinking it was going to cough up the next value,
> not restart the loop.

I probably would not say "restart" the loop.
It goes to the *next* iteration of the loop:


If the loop has a condition it jumps to check that condition again and
acts accordingly.
(If the condition is true it does the next iteration, if the condition
is false it ends the loop.)

use v6;

my $i = 0;

while $i < 6 {
    $i++;
    next if $i %% 2;   # if it can be divided by two go to next iteration
    say $i;
}

prints
1
3
5


If the loop has some action and a condition it will jump to  execute
the action again (increment $i) and check the condition and act
accordingly doing the next iteration or quitting the loop.

use v6;

loop (my $i = 0; $i < 10; $i++)  {
    next if $i %% 2;    # if it can be divided by two go to next iteration
    say $i;
}

will print
1
3
5
7
9


Gabor

Reply via email to