Perl Programming Page 3 - Perl Conditionals |
The Unless Statement is the opposite of the If statement in that it asks if a condition is false, and executes the code unless the condition is true. #!/usr/bin/perl $my_iq = 1000; $your_iq = 90; unless ($your_iq > $my_iq) { print "My brain is enormous and far superior to your own!"; } In the above code, the program will print out the sentence: "My brain is enormous and far superior to your own!" every time, unless $you_iq is greater than $my_iq. Getting Loopy Near the end of the day I always get a little loopy; I can't think straight and words just come out wrong. But I'm getting off topic here. Loops in code are something completely different. The purpose of a loop is to loop a piece of code so that you don't have to type it over a billion times. Let's take a look at the For Loop: #!/usr/bin/perl for ($count =1; $count<10; $count++) { print "I R THE GREATEST!n"; } The above code created a variable name $count and assigned an initial value of 1. It also has a part that tells it to increment the value of $count by +1 ($greatness++) and to do so until the value of $count is 10 or greater ($count<10). So long as the value of $count is less than 10, the following will print to the screen: I R THE GREATEST! I R THE GREATEST! I R THE GREATEST! I R THE GREATEST! I R THE GREATEST! I R THE GREATEST! I R THE GREATEST! I R THE GREATEST! I R THE GREATEST! If we were working with numbers we could have also just assigned a numeric value to $count and have it do this: #!/usr/bin/perl for ($count =1; $count<10; $count++) { print "$countn"; } That would print out the following to your screen: 1 2 3 4 5 6 7 8 9 The For Loop loops for the number of times you tell it to.
blog comments powered by Disqus |
|
|
|
|
|
|
|