In the last tutorial, we learn loop statement in Perl language. In this tutorial, we will show you how we can do interrupt process in the loop statement. Below is the few commands we can use in Perl.
Next Statement
Next statement will start the next iteration of the loop. It can be used inside a nested loop or perform outside of the loop. Let's us see how it can be perform.
Next statement flow |
#!/usr/bin/perl
for($i=0; $i<10; $i++)
{
if($i==5) {next;}
print "Value = $i\n";
}
for($i=0; $i<10; $i++)
{
if($i==5) {next;}
print "Value = $i\n";
}
The above example performs as the video below.
Continue Statement
A continue statement is always executed before the conditional been checked again. Let's us see how it works in our below example.
Continue statement flow |
#!/usr/bin/perl
$i = 0;
while($i<10)
{
print "Value = $i\n";
$i++;
} continue{
if($i==5)
{
print "This next number is 5\n";
}
}
$i = 0;
while($i<10)
{
print "Value = $i\n";
$i++;
} continue{
if($i==5)
{
print "This next number is 5\n";
}
}
The output of this example is shown as video below.
Last Statement
The last statement will terminated the loop immediately, it don't care whether the loop condition is true or not. Let's us see how it works.
Last statement flow |
#!/usr/bin/perl
$var = 0;
while($var<10)
{
print "Value = $var\n";
$var++;
if($var==5) {last;}
}
print "END\n";
$var = 0;
while($var<10)
{
print "Value = $var\n";
$var++;
if($var==5) {last;}
}
print "END\n";
The output of above example is shown as the video below.
Redo Statement
This redo command used to restart the loop block without evaluating the conditional again. It works same as continue command, but it is located inside the loop. Let's us see how it works and what is the difference with continue command.
Redo statement flow |
#!/usr/bin/perl
$var = 0;
while($var<10)
{
print "Value = $var\n";
$var++;
if($var==10) {redo;}
}
$var = 0;
while($var<10)
{
print "Value = $var\n";
$var++;
if($var==10) {redo;}
}
The above example should able to work as the video below.
Goto Statement
Goto is a powerful tool in programming. It can jump from one statement to another statement. Normally, it is been used to execute one expression from another function. Let's see how it works at here.
Goto statement flow |
#!/usr/bin/perl
for($i=0; $i<20; $i++)
{
print "Value = $i\n";
if($i%2==0)
{
print "It is Even\n";
}
elsif($i==15)
{
goto End;
}
else
{
print "It is Odd\n";
}
}
End:
{
print "STOP\n";
}
for($i=0; $i<20; $i++)
{
print "Value = $i\n";
if($i%2==0)
{
print "It is Even\n";
}
elsif($i==15)
{
goto End;
}
else
{
print "It is Odd\n";
}
}
End:
{
print "STOP\n";
}
The above example will be get the output as the video below.
Yeah, we are successfully finish for this tutorial. In next tutorial, we will learn the Perl operation. See you guys. :D
No comments:
Post a Comment