When I am executing a program both giving the same result. Please explain the difference between 'next' & 'redo'.
redo
, next
and last
are used inside loop blocks in Perl. Most often you will see them in for
or while
blocks, but you can use them in a bare block if you need to
The essential difference is
redo
jumps to the beginning of the block -- the opening brace {
next
jumps to the end of the block -- the closing brace }
last
jumps out of the block altogethernext
and last
are only different for the blocks of while
and for
loops (and for blocks that have a continue
section)
So you could write a loop like this
my $n;
{
++$n;
print $n, "\n";
redo if $n < 10;
}
which would print the numbers from 1 to 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With