In some cases user need to execute a block of code many ranges of times. In general, statements are executed sequentially initially the first statement in a function is executed then first, followed by the second, and so on. Programming languages offer numerous control structures that provide additional sophisticated execution methods. The image below demonstrates the flow control of the loops as shown below.
Following are the some Perl programming language Loops as shown below.
While
While loop statement is used to execute the targeted statement repeatedly as long as given condition is true. The syntax of the while statement is shown below.
[code]
while(condition)
{
statement(s);
}
[/code]
If There are a statement and condition may be any expression the loop will start the execution when the condition is true and if the condition becomes false then the program control passes to the line immediately following the loop. The image below demonstrates the Flow diagram of the While as shown below.
The code below demonstrates the While statement as shown below.
[perl]
$a = 10;
# while loop execution
while( $a < 20 ){
printf "Value of a: $a\n";
$a = $a + 1;
}
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
Until
Until loop statement is used to execute the targeted statement repeatedly as long as given condition is true. The syntax of the Until statement is shown below.
[code]
until(condition)
{
statement(s);
}
[/code]
If There are a statement and condition may be any expression the loop will start the execution when the condition is true and if the condition becomes false then the program control passes to the line immediately following the loop. The image below demonstrates the Flow diagram of the Until as shown below.
The code below demonstrates the Until statement as shown below.
[perl]
$a = 5;
# until loop execution
until( $a > 10 ){
printf "Value of a: $a\n";
$a = $a + 1;
}
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
For
For is also known as repetition control structure which allows a user to write a loop to execute a specific number of times. The syntax below demonstrates the For Loop.
[code]
for ( init; condition; increment ){
statement(s);
}
[/code]
In the above syntax, init will execute first and only once which allows a user to declare initialize the control variables. and then the condition is evaluated if it is true then only the loop will execute if it is false the flow of control jumps to next statement which is just after the loop. After the condition flow of control moves to the increment statement which allow user to update the control statement variables. The image below demonstrates the Flow control of For loop as shown below.
The code below demonstrates the For Loop as shown below.
[perl]
# for loop execution
for( $a = 10; $a < 20; $a = $a + 1 ){
print "value of a: $a\n";
}
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
For each
For each loop is used to iterates over a list values and sets the control variable to ech element and the snippet below demonstrates the syntax of the for each.
[code]
foreach var (list) {
...
}
[/code]
The image below demonstrates the Flow control of the for each element as shown below.
The code below demonstrates the For each loop as shown below.
[perl]
@list = (2, 20, 30, 40, 50);
# foreach loop execution
foreach $a (@list){
print "value of a: $a\n";
}
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.