View Single Post
Old 11-22-2003, 04:11 PM   #10 (permalink)
juanvaldes
Banned
 
Location: shittown, CA
Control Structures.

Perl like many languages common today have a varierty of looping structures that are very useful. The keywords are "if", "while" and "for". You can nest these structures inside one another allowing your program to have powerful logic.

Logical expressions are created using relationsal operators. Perl has two types of basic comparison operations, the numeric set and the string set.
<table>
<tr><td>Operation</td><td>Numeric</td><td>String</td></tr>
<tr><td>Is equal to</td><td> == </td><td>eq</td></tr>
<tr><td>Is not equal to</td><td> != </td><td>ne</td></tr>
<tr><td>Is less then</td><td> &lt; </td><td>lt</td></tr>
<tr><td>Is greater then</td><td> &gt; </td><td>gt</td></tr>
<tr><td>Is less then or equal to</td><td> &lt;= </td><td>le</td></tr>
<tr><td>Is greater then or equal to</td><td> &gt;= </td><td>ge</td></tr>
</table>

examples:
$foo = 15;
$bar = $foo;
# same as $bar = 15;

$foo > $bar
# this is false, 15 > 15 is not true.

$foo <= $bar
# this is true, 15 <= 15 is true because of the equal part.

$bar == $foo
# this is true, 15 == 15 is true.

other useful operators include ++, --, &&, and ||.

++ is called "increment" and adds one to your variable.
example:
$foo = 3;
$foo++;
# $foo = $foo+1, so $foo = 4
print $foo; #prints '4'

Decrement is symbolized by --, and does the opposite as increment, subtracting one from the variable each time.
example:
$bar= 3;
$bar--;
# $bar = $bar-1, so $bar= 2

&& is the "and" operator. if the two objects you are anding are both true, then the whole expression is true. if either or both objects are false, the expression returns false. this is a good way to combine checking multiple conditions at once.

|| is the "or" operator. if the 2 objects being compared are both true, or if EITHER of them are true, then the expression is true. the expression is only false if both items are false. this will become useful when we look at control structures.

//Control Structures


When a control structure (such as an if statement, or a while loop) is encountered the system will check the expression and if it evaluates to true the code inside the control structure will be executed. Otherwise it will skip the block of code and continue execution.

Looping structures usually use this pattern for the logical expression: "something is less then something else". Or "something is not equal to something else". There are many variants I will get to shortly. While this is not the only way to use loops it is a very common useage of the structure. So I will wrap this up into a single unit I will call the "control expression".

'control expression' = 'left comparison' 'compare operator' 'right comparison'

Both the left comparison and right comparison can be either variables (Scalars) or hard coded literals (the number 1 or the string "hello") these will always be the same unlike variables which are dynamic and can be different depending upon other code in the system.

Here is the skeleton of the control structures:
Code:
if( 'control expression' )
{
	# your code goes here.
}
If statement's work the way they do in natural language, "if $bob is equal to "hello", then we enter the if block and execute it's instructions, when done we continue down the program. If $bob does not equal "hello" then we skip the entire block and continue.

Code:
while( 'control expression' )
{
	# your code goes here.
	# increment before the next iteration of the loop.
}
A while loop is just like an if statmement but with one difference. After you are done executing the code in the while block, you go back up and check the while control comparison again. This allows you to loop through the same code many times.

Be careful though because if your control expression does not get modified while inside the while loop you may never break free and get stuck in a 'infinite loop'.

Code:
until( 'control expression' )
{
	#code goes here.
}
A until block is just like a while loop but the value of the control expression is used in the opposite way. The loop will run forever UNTIL the control expression returns true.

Because while and until loops are so similer you can convert one to the other by changing the logic of the control structure, it is mearly provided for the convience of the programmer to not have to deal with too many opposites at a time.

Code:
for( 'loop counter' ; 'control expression' ; 'increment counter' ) 
{
	#code goes here.
}
A for loop is a while loop that takes care of itself. The loop counter can be set to any value desiered here. Next is the control expression your familer with. Finally the best part of the for loop over a basic while loop, the increment counter part. You can move the counter up or down in the for loop declaration instead of doing yourself inside the while loop. This results in cleaner code, the meat of the logic is inside the loop less lines that power the loop getting in your way and reducces the number of stupid infanite loop mistakes.

A for loop can be convired to a while loop like this.

Code:
for($bob = 0; $bob < 10; $bob++)
{
	# code here.
	# each time the loop executes, bob is increased by one.
}

# is the same as...
$bob = 0;
while($bob < 10)
{
	#code here.
	$bob++;
}
# Both of those loops do the exact same thing.
A few more examples...

Code:
$foo = 42;
if($foo > $input)
{
        print "The input is less then the value 42.";
}
Every one of these loops will output the same string 5 times.
Code:
#!/usr/bin/perl

$foo = 5;
print "While loop:\n";
while($foo > 0)
{
       print "We will output now\n";
       $foo--;
}
print "Until loop:\n";
until($foo >= 5)
{
        print "We will output now\n";
        $foo++;
}
print "For loop:\n";
for($foo = 0; $foo < 5; $foo++)
{
       print "We will output now\n";
}

Last edited by juanvaldes; 11-24-2003 at 06:21 PM..
juanvaldes is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76