while loop allows to execute blocks of statement based on a conditional boolean expression.
Perl language provides the following two kinds of while loops.
- Perl While Loop
- Perl Do While Loop
- Perl Until loop
Perl While Loop example
It executes the code statement multiple times based on the bool value.
Here is the syntax of a while loop.
Label_name: while (conditional expression) {
code block statements
}
Label_name is an optional name for the label.
while
is a keyword in Perl that can not be used as a variable name.
conditional-expression
is a Perl expression evaluated to boolean values in Perl only. if Condition is satisfied, Statements inside a while loop are executed.
the conditional expression must enclose in (()
).
code block contains a single line or block(multiple lines) of code. These must be enclosed in {}
,
Here are while loop examples in Perl.
my $number = 10;
my $sum = 0;
while ($number >= 1) {
$sum = $sum + $number;
$number--;
}
print "The Sum is $sum";
Output:
The Sum is 55
Perl Do While Loop example
Do While
is to execute multiple blocks of code based on conditional expression value.
Do While Loop
is similar to while loop
except that checking condition expression is done at the end i.e. first iteration is completed. That means code statements are executed at least once.
The conditional expression always results in the boolean values in Perl.
if-else statements are used to execute statements based on true and false values.
Here is the syntax of the do-while loop.
do {
//statements1
}while(conditional-expression1)
conditional-express results in a value of boolean value Perl.
code statements are executed at least once.
if the condition is satisfied, It iterates multiple times and executes code statements.
do
and while
are keywords that can not be used as identifiers in Perl
Here is an example code for the do-while loop in Per
my $number = 10;
my $sum = 0;
do {
$sum = $sum + $number;
$number--;
}while ($number >= 1);
print "The Sum is $sum";
Perl Until Loop example
until loops are special loops similar to the while loop. It executes the code block until the condition is true in Perl
my $number = 10;
my $sum = 0;
until ($number >= 1) {
$sum = $sum + $number;
$number--;
}
print "The Sum is $sum";
When do we use to do while instead of while loop?
Both are used to execute multiple code statements based on conditional expressions in Perl. do while loop executes code statements at least once. if the condition is passed, It executes multiple code statements.
While the loop will not execute code statements until the condition is passed.
How do you write a while loop in Perl?
To write a while loop in Perl, Please follow the below steps
- while keyword followed by a conditional expression enclosed in
{}
- Code statements are written inside a
{}
- These statements are executed based on conditional expression in a while loop
What is while in Perl?
while is a keyword in Perl that is not used as a variable name or identifier.
It is used to execute code block statements based on a conditional expression. For example, Arrays and hash are used to iterate elements.