top of page

Control Statements

Decision Statements

Decision statements use the if keyword to execute a statement block based the evaluation of an expression or to choose between executing one of two statement blocks based the evaluation of an expression.


Example: The If Statement

The syntax for the if statement is the following:

if (CONDITION) {
    # Code block executed
    # if condition is true.
} else {
    # Code block executed
    # if condition is false.
}

Sometimes you need to choose from multiple statement blocks, such as when you need to execute a different statement block for each month. You use the if...elsif statement for this type of decision. The if...elsif statement has this syntax:

if (CONDITION_ONE) {
    # Code block executed
    # if condition one is true.
} elsif (CONDITION_TWO) {
    # Code block executed
    # if condition two is true.
} else {
    # Code block executed
    # if all other conditions are false.
}

#Subtract five from $firstVar and if the resulting value not zero then execute the statement block.

$firstVar = 10;
if ($firstVar -= 5) {
    print("firstVar = $firstVar\n");
}

firstVar = 5


#Subtract five from $firstVar and if the resulting value is not zero then print $firstVar. If not, print "firstVar is zero".

$firstVar = 5;
if ($firstVar -= 5) {
    print("firstVar = $firstVar\n");
} else {
    print("firstVar is zero\n");
}


firstVar is zero


#To print a food chart for Breakfst lunch and dinner.

$foodchain = "breakfast";
if ($foodchain == breakfast) {
    print("8 AM to 11 PM\n");
}
elsif ($foodchain == lunch) {
    print("12 PM to 3 PM\n");
}
elsif ($foodchain == dinner) {
    print("7 PM to 9 PM\n");
}
else {
    print("Chart not available\n");
}
February


Loop Statements

A loop is used to repeat the execution of a statement block until a certain condition is reached. A loop can be used to iterate through an array looking for a value. Loops can also be used to count quantities. Actually, the number of uses for loops is pretty much unlimited. There are three types of loops: while loops, until loops, and for loops.


Example: While Loops

While loops are used to repeat a block of statements while some condition is true. There are two forms of the loop; one where the condition is checked after the statements are executed (the do..while loop) and one in which the condition is checked before the statements are executed (the while loop).
The do...while loop has this syntax:


do {
    STATEMENTS
} while (CONDITION);
The while loop has this syntax:

while (CONDITION) {
    STATEMENTS
}
continue {
    STATEMENTS
}

#Prints number 1 to 10
$num = 1;
while( $num <= 10 ){
   printf "$num\n";
   $num++;
}


The statement block of a do...while loop always will be executed at least once. This is because the condition is checked after the statement block is executed rather than before. Here is an example of the do...while loop.

#Prints number 10th table
$firstVar = 10;
$i = 1;
do {
    print ($firstVar*$i);
     print "\n";
    ++$i;
} while ($i < 10);

 

Example For loops:


One of the most common tasks in programming is looping a specific number of times. Its syntax is:

for (INITIALIZATION; CONDITION; INCREMENT/DECREMENT) {
    STATEMENTS
}


The initialization expression is executed first - before the looping starts. It can be used to initialize any variables that are used inside the loop. Of course, this could be done on the line before the for loop. However, including the initialization inside the for statement aids in identifying the loop variables.


for ($i=0 ; $i<=10 ; $i++)
{
print "$i \n";    
}

Example: Foreach Loops

Arrays are so useful that Perl provides a special form of the for statement just for them. The foreach statement is used solely to iterate over the elements of an array. It is very handy for finding the largest element, printing the elements, or simply seeing if a given value is a member of an array.

foreach LOOP_VAR (ARRAY) {
    STATEMENTS
}


The loop variable is assigned the value of each array element, in turn until the end of the array is reached. Let's see how to use the foreach statement to find the largest array element.


@max = (1,2,3,4) ;
$i= 0;
foreach $k (@max) {
print "$k \n";
++$i;
}

© 2015 Thirumurugan

bottom of page