Processes in coding can come in three different formats; 1. [[Conditions and Loops#Instantiative Algorithms|Instantiative Algorithms]] 2. [[Conditions and Loops#Conditional Algorithms|Conditional Algorithms]] 3. [[Conditions and Loops#Iterative Algorithms|Iterative Algorithms]] # Instantiative Algorithms **Instantiative algorithms** are those that occur straight through and have no conditions that need to be met, nor any repetitions. HTML documents are instantiative algorithms in that they simply are read and displayed without condition and without repetition. # Conditional Algorithms **Conditional algorithms** are those that will only perform certain tasks if a specific condition is met. It is managed with `if ... else` statements. ## Syntax ```perl if ($condition == 1) { # the condition can be any comparison # perform certain commands } elsif ($differentCondition) { # perform different commands } else { # perform completely different commands } ``` ### Conditions evaluate to either "true" or "false" "True" and "False" in PERL and most other languages are evaluated as either `1` or `0`. If a condition returns `1`, it will run the code. If it returns `0`, it will go to the next `elsif` to see if it meets that condition instead. You can have unlimited `elsif` statements, and eventually if none of them are met, it will look for the `else` statement to catch all other options. ```perl ($lowNumber == $highNumber) #will return 0 - false ($lowNumber > $highNumber) #will return 1 - true ($lowNumber != $highNumber) #will return 1 - true) ($lowNumber) #will return 1 if there $lowNumber has any value. (!$lowNumber) #will return 1 only if there is no value or is 0. $myString = "HELP"; ($myString eq "HELP") #will return 1 because it matches exactly ($myString eq "Help") #will return 0 as it's not an exact match ($myString = "Thanks") #this is not a comparison, it's a command. If the server was able to complete the command, it will return a 1. If it was not able to do it, it will return a 0. ``` > CHALLENGE: Create a PERL script that will print a simple HTML page with a simple white background *unless* the user also includes a color as a parameter, at which point it should print that HTML page with that background color. # Iterative Algorithms **Iterative algorithms** are those that will happen multiple times in exactly the same way. It will loop until you tell it not to. There are two ways to do so, one is the `while` loop, the other is the `for` loop. ## while Loops The `while` loop will continue to do the same code over and over again until a condition is met. An example is below; ```perl $counter = 0; while ($counter < 10) { #the condition is checked on every loop. print "Hello"; $counter++; #increases the value of $counter by one every time. } ``` If `$counter` isn't changed directly by you, it will create an infinite loop that will kill your server, so it is very important that you do something to change the condition to make the loop eventually stop happening. The Result: ``` HelloHelloHelloHelloHelloHelloHelloHelloHelloHello ``` > CHALLENGE: Create a PERL script that prints the multiples of any value sent by the user in a parameter, up to `n x 25`. ## for Loops The `for` loop has a built-in exit because it will loop through a list of information. That list can come in many different formats. An example of a simple `for` loop is below; ```perl for (1..10) { #the list is from 1 to 10 print "Hello"; } ``` This produces the same result as demonstrated with the `while` loop above. ### Other Lists You can make a numerical list, or you can use lists of data you already have. For example, you can loop through keys of a `hash` or a list of scalars called an `array`. The first example is with a `hash`. ```perl %myHash = (); $myHash{'blue'} = "bleu"; $myHash{'red'} = "rouge"; for (keys %myHash) { #go through the list of keys in the hash print "$_ is the key, $myHash{$_} is the value associated with it.\n"; #in loops, the current item in the list is given the #standard variable $_. You can also change the name of the #standard variable as shown in the next example. } ``` The Output: ``` blue is the key, bleu is the value associated with it. red is the key, rouge is the value associated with it. ``` This example is with a `list` or an `array` (these words are interchangeable). ```perl @myList = ("Hello","my","name","is","Sparticus"); for $thisOne (sort @myList) { #go through the list in alphanumeric order print "$thisOne "; #in the hash example above the $_ standard variable was used. #This time we gave a specific variable for the current item #to go into - $thisOne. } ``` The Output: ``` Hello is my name Sparticus ``` > CHALLENGE: Create a PERL script that gets a series of key-value pairs from the parameter, and prints the results to the screen regardless of what key-value pairs the user includes.