For loops.pl

From JaxHax
Jump to: navigation, search

for_loops.pl Overview

The for_loops.pl gives a basic example of how to use a for loop in perl. For loops are useful for repeative task or for creating loops to process an unknown number of iterations that will be determined at runtime by the user or the data they are trying to process with it.


for_loops.pl Code

Save the code below in a file called for_loops.pl:

#######################################################
#
# Example: for_loops.pl
#
# Here we will cover FOR loops. The syntax for these
# are pretty straight forward, It takes 3 arguments:
# 
#       for(initialization; test; increment){
#	   statements;
#	}
#
# The initialization, set a variable and it's value
# for this loop
#
# Test is the condition that while it is true, we keep 
# going.
#
# the increment is what it does at the end of the loop.
# 
########################################################
 
###
#### So let's write a simple Blast off count down.
#### basically we will count down from 5 to 1 then
#### blast off with a one second pause between them.
####
#### for the increment you notice i'm using $i--. This
#### means take the current value of $i and subtract 1.
#### you could also use $i++ as well to add one instead,
#### but make sure to adjust the initialization so you're
#### not stuck in an endless loop counting away from the
#### test condition.
###
print "\n\nBlast off in:\n\n";
 
for ($i = 5; $i > 0; $i--) {
	print"$i\n";
	sleep(1);
}
 
print "\nBlast Off!\n\n";


Running for_loops.pl from a Shell

[user@localhost]$ perl for_loops.pl 
 
Blast off in:
 
5
4
3
2
1
 
Blast Off!
 
[user@localhost]$


Code Breakdown

So this example is used to print a blast off count down, since there are 5-1 counts printed with a delay, we could have used 10 lines of code for the print and sleep statements manually; Or you can think like a programmer and work smarter, not harder, and replace those 10 lines with 4, and those 4 lines count cover a 5 count or a 5 million count all the same; just a quick change in the parameteres in the for loops declaration. On the first line of code we call a simple print statement, this works alone since its a one time deal and not repeating. Therefore it stays out of the for loop.


For loops are declared in the following manner:

#  for ([initialization]; [test]; [increment]) {


Which leads us to our for declaration in our script:

for ($i = 5; $i > 0; $i--) {

So let's go over what is declared here inside our for loop expression. First up is our [initialization] statement of $i = 5. We are telling perl to set the variable $i to the value of 5 before it starts the loop. This is only done once before entering the loop. The next parameter which is our [test] parameter is $i > 0. The test parameter is an expression that will be evaluated at the end of the loop iteration to determine if it needs to re-enter the loop or if it is done and should continue code execution outside of the loop (that is to say, exit the loop and proceed down the script). The third parametered is the [increment] value which in this case is $i--. The [increment] value is something that occurs at the end of the loop before the [test] expression is evaluated. The $i-- will decrease the value of $i by one, which is perfect for our countdown. However there is also $i++ which would increment it by 1 as well. These methods are a sort of tribute to C where a variable followed -- or ++ would increment or decrease by 1. It's also important to note that you aren't limited to these! Any mathmatical express would work here as well. For example let's say you wanted to decrease it by two instead of one each iteration, you can replace "$i--" with "$i = $i - 2" instead and it will work just find with no syntax errors. Also if you are wondering why we call the variable $i, this is shorthand convention for loop "iterator", which is common practice across alot of languages.


The next two lines are our print and sleep lines inside the for loop:

	print"$i\n";
	sleep(1);

These are what gets executed when our loop iterates each time. Notice how we are using $i in the print statement. It decreases everything we run through the loop due to the [increment] value we setup in the declaration. This is great as this variable almost serves to use as usable tab counting down the number of times remaining. Each iteration of the loop we will print the number in $i and wait for one second (this is caused by the sleep(1), sleep means just sit there and do nothing and the parameter it takes in is the number of seconds to wait, in this case it is 1), you know this sleep(1) is VERY important...or its just for dramatic effect ;-)


Once we have completed five iterations in that loops from 5 down to 0 ($i will go to zero, but that fails the test condition and makes it exit the loop, $i = 1 will execute because 1 > 0 = true, remember it is ">" at work here, not ">=") we have one more print statemenet that just confirms the blast off.