Monday, July 18, 2011

For Loop perl

For loop is mainly used to loop through the STATEMENTS a specific number of times.
You can use it when you want to execute statement specific number of times.


SYNTAX:

For loops:
for(INITALIZATION;CONDITION;INCREMENT/DECREMENT)
{
STATEMENTS
}

In this case the intialization happens first,and the condition is checked,when the condition is passed it executes the statements,Else it comes out
And after executing the Statement,it increments or decrements and checks the condition.If condition passes it again executes else it
comes out.


Example:

for ( $var = 0; $var < 10; $var ++) { print "The var value is $var"; }

Output:

G:\perl_programs>perl perl_for.pl
The var value is 0
The var value is 1
The var value is 2
The var value is 3
The var value is 4
The var value is 5
The var value is 6
The var value is 7
The var value is 8
The var value is 9

G:\perl_programs>

No comments:

Post a Comment