Apr 12, 2008

Perl Program Control and Loop

Perl Program Control

Provides information about Perl statements which are used for program control including the if statement, for statement, while statement, and others.

If Statements

The if statement uses the following comparisons:

Symbol

Meaning

==

Equals

!=

Not equal to

<

Less than

>

Greater than

<=

Less than or equal

>=

Greater than or equal

An example:

if ($thisnum == 4)
{
        print $msg1, "\n";
}

For Statements

The following statement counts from 1 to 5 print the results on a new line for each loop.

for ($count = 1; $count < 5; $count++)
{
        print $count, "\n"; 
}

While Statements

$count = 0;
while ($count < 7 )
{ 
        print $count, "\n";
        $count++;
}

Foreach statements

This statement is used to operate on arrays as in the following example.

foreach $cookie (@cookies)
{ 
   print $cookie, “\n”;
}

The @cookies value is an array and the $cookie scalar variable is set to a value of a member of the array and the loop continues until all members of the array have been operated on. Therefore the above example will print every member of the @cookies array.

No comments:

Post a Comment

Popular Posts