Home >> perlloops >>The Loop StatementsDifferent loop statements in perl...
The for Loop This loop is used to execute a given set of statements for a fixed number of times. The syntax of the for loop is: for(initialization;testing;updation)
{
block of statement(s);
}
In these statements: initialization: Is the code to declare and initialize the loop counter. Loop counter is a variable to keep a check on the number of iterations. Initialization happens only once before the beginning of the loop. testing: Is the code, which specifies the condition to control the number of iterations. The loop executes until the result of testing is true. When the condition becomes false, the control passes to the statement following the loop. updation: Is the code to modify the loop counter after each iteration. It can increment or decrement the loop counter, according to the program requirements. Updation occurs at the end of the loop. block of statement(s): Is the code to be executed iteratively. This code must be enclosed within the curly braces. Note The three expressions for initialization, condition, and updation are optional. If you leave the condition expression empty, the for loop will be an infinite loop. #! /usr/bin/perl
print "Enter a digit to create its table: ";
$a = <>;
chomp($a);
for($b=1;$b<=10;$b++)
{
print $a.' x '.$b.' = '. $a*$b."\n";
}
The Nested for Loop A for loop contained inside another for loop is called a nested for loop. This is used when the data is to be stored and printed in a tabular format having multiple rows and columns. #! /usr/bin/perl
for($a=0;$a<=9;$a++){
for($b=0;$b<=$a;$b++){
print "*";
}
print "\n";
}
In this program:
The foreach Loop This loop operates on arrays. An array stores multiple related values in a row that can be accessed easily using the foreach loop. The syntax of the foreach loop is: foreach $var_name (@array_name)
{
block of statement(s);
}
In this syntax:
#! /usr/bin/perl
@names = ("George", "Jack", "Davis");
foreach $word(@names)
{
print "$word\n";
}
This example, when executed, prints values of all the elements in the @names array one-by-one using the foreach loop. The output of the example is shown in Figure 4-5: The while LoopThere may be situations when you do not know the number of times a loop is to be executed. For example, an application accepts and stores the scores of students in a class, and you do not know the number of students in a class. In this example, you can use the while loop.The while loop executes as long as the condition specified is true. The condition can be any valid relational expression, which returns true or false. This loop is also known as Pre-Check or Pre-Tested Looping Construct because the condition is checked before executing the statement(s) in the block. The syntax of the while loop is: while (condition)
{
block of statement(s);
}
In these statements, block of statement(s) is executed only if the condition is true. Note The code block must be enclosed within curly braces. #! /usr/bin/perl
$a = 1;
while ($a <= 10)
{
print "$a\n";
$a++;
}
In this example:
The do-while LoopThe do-while loop is used when you want to execute a code block at least once unconditionally, and then iteratively on the basis of a condition.In this loop, condition is tested at the end of the loop. Because of this, this loop is also known as Post-Check or Post Tested Looping Construct. The syntax of the do-while loop is: do
{
block of statement(s);
}
while (condition);
#! /usr/bin/perl
$a = 2;
do
{
print "$a\n";
$a+=2;
} while ($a<=20);
In this example:
The until LoopIn case of the while loop, the code that follows condition is executed only if the condition is true. In the case of the until loop, code associated with the condition is executed only if the condition is false. The syntax of the until loop is:until(condition)
{
block of statement(s);
}
In this syntax, the block of statement(s) is executed only when the condition returns false. #! /usr/bin/perl
$a = 1;
until(a == 11)
{
print $a."\n";
$a++;
}
In this program:
Perl functions
|
|