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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360