shadowline
Hashes << Conditionals >> Controls
Conditional statements are different ways of checking an expression to see if it is true (or not) and then react based on the results.

IF will execute a statement block upon a true expression value.

if (expression)
   {do this statement block only if the expression is true;}

If the expression had a false value, then that statement block would not be executed and the script would go onto the next line of code. What about the times when you want a different statement block to be executed during a false value and THEN go onto the next line of code? That is where the ELSE addon comes in.

if (expression)
   {do this statement block only if the expression is true;}
else
   {do this statement block if the expression is false;}

Quick example :
$a=25;
if ($a=10)
   {print "a is equal to 10.";}
else
   {print "a is not equal to 10.";}

Result : a is not equal to 10.

ELSIF statements can be used when you have more than one expression to evaluate and to select the correct statement block to execute. When any statement block within the control structure is executed, the control is taken out of the IF area and continues onto the next line of code. The balance of the IF area is ignored. You can have as many elsif statements in an control structure as required.

*NOTE* : be sure to spell ELSIF correctly! There is no E in the middle as in ELSEIF.

if (expression)
   {do this statement block only if the expression is true;}
elsif (expression)
   {do this statement block only if the elsif expression is true;}
else
   {do this statement block if the expression is false;}

If the IF expression is true, it will execute the first statement block.
If the IF expression is false, it will test the ELSIF expression.
If the ELSIF expression is true, it will execute it's statement block.
If the ELSIF expression is false, the control continues to the next ELSIF, or ELSE, or end the control structure (whatever is next after the ELSIF).



The IF command is mostly used to find a TRUE expression. The UNLESS command is used to find a FALSE expression. You can actually find a false expression using the IF command as well, but that's another story.

unless (expression)
   {do this statement block only if the expression is false;}

The ELSE command can also be used with UNLESS command and will react like it did on the IF command.

unless (expression)
   {do this statement block only if the expression is false;}
else
   {do this statement block if the expression is true;}



The WHILE conditional statement lets you repeat a statement block as many times as you need. It works in a similar way as the IF ELSE. WHILE will run its statement block if an expression value is TRUE.
while (expression)
   {do this statement block as long as the expression has a true value}

WHILE creates a loop effect, so a scalar can be used to control the number of loops like a counter.
$counter=15;
while ($counter < 25)
   {print "$counter is less than 25. <br>"; $counter++;}

While the value of $counter is less than 25, a sentence is printed.
The value of $counter is increased by one and the expression is re-evaluated.
The loop (re-evaluation) will continue until a FALSE result breaks it.


UNTIL is the opposite of WHILE, but the end result is very similar. It will repeat a statement block "until" the expression is true.
$counter=15;
until ($counter > 25)
   {print "counter has a value of $counter. <br>"; $counter++;}

Compare the WHILE and UNTIL examples. If you test them out, you will notice that the WHILE did 10 loops and UNTIL did 11 loops. The choice of which one to use will depend on your specific application and the result you are looking for.



The DO WHILE conditional statement allows you to execute a statement block at least one time and continue its loop as long as the expression remains true. The expression is evaluated at the end of the loop instead of at the start.
$counter=15;
do {print "The value of counter is $counter. <br>"; $x++;}
while ($counter < 25);

The DO WHILE comes in handy if you want a specific block executed at least once no matter what the value holds. Change the last line to while ($counter<10); and see what happens. Notice there is a semicolon added at the end of the WHILE line. This shows it belongs to a DO WHILE loop instead of a regular WHILE loop.


The FOR conditional provides a shorthand method of specifying how many times a statement block is to be executed. It follows a different layout than the above conditional statements :
for (initial; expression; iterator)
   {statment block coding here}

The looping control is all kept in the expression area instead of within the statement block area. Nice, tidy and in control. Don't let those foo words spoil the fun. Initial is just where you want the loop to start. Expression is just like you've seen in all the other examples. Iterator is the counter part.

for ($counter=15; $counter<25; $counter++)
   {print "The value of counter is $counter. <br>";}

During each loop, the conditional statement is re-evaluated, the iterator re-increases, and so forth until the conditional statement is false.



FOREACH is often used to find values within an array structure. The control will go to each value within a specified array, assign that value to a temporary scalar, and use the value for whatever purpose.

$x = 10;
print "The value of x before the loop is $x. <br>";

@y = (1,8,6,5,7);

foreach $x (@y)
   {print "The value of x inside the loop is $x. <br>";}

print "The value of x after the loop is $x";

See it in action!

So the original value of $x was not altered. The $x in the FOREACH loop is a separate $x of the main script even though it holds the same name. This is known as a local variable. The outer $x is called a global variable.

For an easier approach, you can take out the scalar variable and an implied variable will be used.

@y = (1,8,6,5,7);
foreach (@y)
   {print; print "<br>";}

See it in action!

The first print assumes there is a variable holding a value and will print it. Perl automatically knows you are referring to an "implied" variable when you don't give the print command any variables. The second print statement is just entering a line break for easier output viewing.
Hashes << Conditionals >> Controls
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow