Updated April 3, 2023
Introduction to Perl last
Perl last statement is the same work as a break statement in c programming. When we use the last statement in our Perl program statement will be encountered inside the loop, the executed loop structure is immediately terminated and control of the program is goes to execute the next statement of code. We can use the label with the last statement while using a label with the last statement that can be used with the inside loop which applies to the nearest loop of the code if we don’t specify a label with the last statement in Perl language.
Syntax
Below is the syntax of the statement is as follows.
-
Last;
-
Lable;
Below is the parameter description of the above syntax are as follows.
- Last: The last statement is very important and useful in Perl language. The last statement is used to terminate the loop statement immediately and sends the control to execute the next statement of a program.
- Label: Label is used with the last statement in the language. While using a label with the last statement in Perl we can use it with the inside loop which applies to the nearest loop of the code if we don’t specify a label with the last statement in the language. The label is very important and useful with the last statement.
How last works in Perl?
Below is the working of the last statement in the language is as follows.
- The last statement is very useful and important in the Perl language to break the loop statement and execute the next statement.
- The last statement is also used with the label statement in Perl language.
Below is the flow diagram of the last statement.
- In the above flow diagram shows the step by step execution of code. This is a pictorial representation of a program that how code executes while using the last statement in Perl language.
- If we have a written condition code of program if the condition of the program is true then it will execute block under the condition.
- It will execute at the time when written condition block is true and if the condition is false control goes to execute the last block of the code.
- If our program condition is false then control of the program is going to the last statement of code.
- If we want to terminate the execution of the program we have using the last statement in our code. While using last statement control will execute the next statement of the code.
- The last statement is used to terminate the loop statement immediately and sends the control to execute the next statement of a program.
- The last statement is very important and useful in Perl language.
- While using a label with the last statement in Perl we can use it with the inside loop which applies to the nearest loop of the code if we don’t specify a label with the last statement in Perl language.
- The label is very important and useful with the last statement in the Perl language. The label is used with the last statement in the Perl language.
- In many languages, we have used a break statement to terminate the loop of the program. In the Perl language, we have used the last statement.
Examples
Below is the example of the last statement in Perl language is as follows. In the below example we have used the last statement without any labeled statement are as follows.
Example #1
In the below example we have used the last statement without using the label statement in Perl language are as follows.
Code:
use warnings;
use strict;
my $sum = 0; # Defining sum variable for addition of x and y.
my $x = 0; # Defining x variables
my $y = 0; # Defining y variables
while(1) # Starting of while loop.
{
$sum = $x + $y;
$x = $x + 2;
if($sum > 100) # Condition to end the loop
{
print "Sum = $sum\n"; # Initialization of sum variable.
print "Exiting loop\n";
last; # Last statement is used to terminate the loop statement.
}
else
{
$y = $y - 1;
}
}
print "Loop ended";
Output:
Explanation: In the above example we have to declare x, y, and sum variables to display the sum of x and y value. We have a defining value of x and y is 0 at the time of defining the value of the variable. Then control of loop goes to the while loop in while loop we have initialized value of x and y variable. After while loop control goes to the if condition in if condition control checks the condition of values. After getting the value of sum is 100 last statement break the current execution of the loop and executes the next statement of code.
Example #2
In the below example we have used the last statement without using the label statement in the language are as follows.
Code:
use warnings;
use strict;
my $x = 1; # Defining x variables
my $sum = 0; # Defining sum variable for addition of x and y.
# Outer Loop of the code
Label: while($x < 32) # Using label to the while loop
{
my $y = 1; # Defining y variables
# Label2 defines in inner loop of the program code
Label2: while ($y < 16)
{
my $sum = $sum + $y; # Initialization of sum variable.
if($x == 16)
{
print "Sum is : $sum";
last Label; # Last statement is used to terminate the loop statement with label.
}
$y = $y * 2;
}
$x = $x * 2;
}
Output:
Explanation: In the above example we have to declare x, y, and sum variables to display the sum of x and y value. We have a defining value of x and y is 1 at the time of defining the value of the variable. We have used a label and label2 with the last statement in the Perl language.
Conclusion
The last statement is used to break the current execution of the loop and execute the next statement of the loop, it is the same work as a break statement in another language. The last statement is very important and useful for the language. We can use the last statement with a label in the language.
Recommended Articles
We hope that this EDUCBA information on “Perl last” was beneficial to you. You can view EDUCBA’s recommended articles for more information.