Close
Close

for and each Loops in Perl


If you are here, this means that you are going to be a coder soon. You will solve real-world computer problems, make your own programs, and a lot more. Yeah, sounds good!

Same as 'while', for is also a loop.

Let's go to an example to print 'Hello World' 10 times.

We can also do this with for loop. But before that, let's look at the syntax of for loop.

for(initialization; condition; propagation)
  {
    statement(s)
  }

for ( $a = 1 ; $a <= 10 ; $a ++ )
   {
     print ( "Hello World\n" ) ;
   }
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Now, let's see how for loop works.

for($a=1; $a<=10; $a++)

$a=1 - This step is used to initialize a variable and is executed first and only once. Here, 'a' is assigned a value 1.

$a<=10 - This is the condition which is evaluated. If the condition is true, the statements written in the body of the loop are executed. If it is false, the statement just after the for loop is executed. This is similar to the condition we used in 'while' loop which was being checked again and again.

$a++ - This is executed after the code in the body of for loop has been executed. In this example, the value of 'a' increases by 1 every time the code in the body of for loop executes. There can be any expression here which you want to run after every loop.


In the above example, firstly '$a=1' assigns a value 1 to 'a'.

Then the condition '$a <= 10' is checked. Since the value of 'a' is 1, therefore the code in the body of for loop is executed and thus 'Hello World' gets printed.

Once the codes in the body of for loop are executed, step $a++ is executed which increases the value of 'a' by 1. So now the value of 'a' is 2.

Again the condition 'a<=10' is checked which is true because the value of 'a' is 2. Again the codes in the body of for loop are executed and 'Hello World' gets printed and then the value of 'a' is again incremented.

When the value of 'a' becomes 10, condition '$a <= 10' is true and 'Hello World' gets printed. Now, when $a++ increases the value to 'a' to 11, the condition '$a<=10' becomes false and the loop terminates.

Don’t you think it’s just a different form of while loop? Yes, it is actually.

for and while in Perl

There is another way to write program of for loop

  $ a = 1 ;
  for ( ; $a <= 10 ; $a ++ )
   {
    print ( " Hello World " ) ;
   }

And we can also write it as

  for ( $a = 1 ; $a <= 10 ; )
   {
    print ( " Hello World " ) ;
    $a ++ ;
   }

Try these once.

Using for with range


We can use for loop in a bit different and handy way to iterate over every element of a range. Let's take an example to understand this:

for $a (1..5)
   {
     print $a,"\n";
   }
Output
1
2
3
4
5

Here, 'a' is a variable which will go to every element in (1..5) and will take its value.
Thus, in the first iteration, the value of 'a' will be 1.
In the second iteration, 'a' will be 2 and so on.
And that's it. It is that simple.

foreach loop


foreach loop is used to iterate over every element of an array in a very handy way. Its syntax is similar to using for loop on range as done in the previous example. In foreach loop also, we use a variable to take the value of every element of the array. Let's see its example.

@a = (1,3,5,6,9);
foreach $x (@a)
   {
     print $x,"\n";
   }
Output
1
3
5
6
9

Here, 'x' will go to every element of array 'a' and take their values.
So, in the first iteration, 'x' is 1.
In the second iteration, 'x' is 3 and so on.

Example of sum of marks

@marks = (78,98,50,37,45);
$sum = 0;
foreach $m (@marks){
  $sum = $sum+$m
}
print "$sum\n";
Output
308

I hope you got the code. If not, then let me explain a bit.
$sum = 0 We are taking any variable and assigning it an initial value of 0.
In the 1st iteration
m is 78. So, $sum+$m is 0+78. Now sum is 78
In the 2nd iteration
m is 98. So, $sum+$m is 78+98. Now sum is 176.
Similarly at last, sum is 308.
I hope it is clear now.

Do You Know ?

Table of 12 Add And you get table of 13
12 +1 13
24 +2 26
36 +3 39
48 +4 52
60 +5 65
72 +6 78
84 +7 91
96 +8 104
108 +9 117
120 +10 130

So, let's do it with the help of for loop.

@table_12 = (12,24,36,48,60,72,84,96,108,120);
@table_13 = ();
$z = 1;
foreach $i (@table_12){
  push(@table_13,$i+$z); #using push to add an element in front of list
  $z = $z+1;
}
print "@table_13\n";
Output
13 26 39 52 65 78 91 104 117 130

$z = 1 - We have to add 1,2,3... respectively to each element. And that's what we are doing. Add 1 and then increase z by 1 for the next turn ($z = $z+1)
push(@table_13,$i+$z) will add (12+1),(12+2),...(12+10) in their respective turns in the array.
By writing codes, your mathematics is going to be fun.

We can also use commands like next, last, etc with for loop as used with the while loop.


Ask Yours
Post Yours
Doubt? Ask question