Control Structures


Control structures direct which line(s) of code is to be executed next. They are commonly conditional statements whose outcome depends on the truth or falsehood of a logical expression. Perl supports the control structures that you would commonly find in other languages.


if-then

Here's a simple if-then statement:

if($x > 3) {
# if true, execute some statements here;
$was_block_entered = "Yes";
$y = $z;
}


If the value of $x (interpreted as a number) is greater than 3, then the two statements between the braces {} are executed. If $x is less than or equal to 3, the two statements are not executed. Notice that there is no explict use of the word then in the if-then conditional.

For any type of control statement, braces are used to enclose a block of statements that are to be executed together. Braces are required for a control statement.


Determining Truth

Unlike some programming languages, there is no boolean data type in Perl. In addition to results of logical expressions, other expressions and variables and can be evaluated as true or false. The following values are evaluated as false:

"" # the null string
"0" # the ASCII zero character
0 # the number zero

All other values are evaluated as true.

For the conditional below, here are how the following values would be evaluated:

if($a){
   print $a;
}

$a = "Joe";       # True expressions.  Would be printed.
$a = 3;
$a = "00";
$a = ".0";

$a = 7 + 3 - 10;  # False expressions.  Would not be printed.
$a = "";
$a = 0;
$a = "0";


if-then-else

The if statement can optionally have an else block that will execute if the conditional expression evaluates to false.

Example:

if($a > 0){
	print "value is greater than zero";
}
else {
	print "value is not greater than zero";
}


if-elsif

If the first condition is not evaluted as true, a second, third, or any number of alternate conditions can be offered with the elsif construct.

Example:

if($a > 0){
	print "value is greater than zero";
}
elsif($a == 0){
	print "value equals zero";
}
else{
	print "value is less than zero";
}


for Loops

Using the for statement allows a block of intstructions to be repeated for a number of cycles. It has the form:

for( $loop_variable; conditional expression; modify $loop_variable) {
   # statements here - inside the braces - are executed every pass 
   # through the loop while the conditional expression is still true
}

Example:

for($index = 3; $index < 6; $index = $index +1) {
	print "Index is $index \n";  
}

Output:

Index is 3
Index is 4
Index is 5


while Loops

The while statement has a different syntax for looping.

while(conditional expression ){
   # statements here - inside the braces - are executed every pass 
   # through the loop while the conditional expression is still true
}

Example:

$index = 3;
while($index < 6) {
	print "Index is $index \n";  
	$index = $index +1
}

Output:

Index is 3
Index is 4
Index is 5

Note: If the conditional is evaluated as false the first time through, the statements in the block are never executed. For example, if $index was initialized to 6. This is also true of the for statement.



See the Perl Desktop Reference or the standard Perl Documentation for other types of control statements and further variations on the above.