|
In this episode of the Perl tutorial we are going to see how does the while loop work in Perl.
use strict;
use warnings;
use 5.010;
my $counter = 10;
while ($counter > 0) {
say $counter;
$counter -= 2;
}
say 'done';
The while loop has a condition, in our case checking if the variable $counter is larger than 0 and then a block of code wrapped in curly braces. When the execution first reaches the entrance of the while loop it checks if the condition is true or false. If it is FALSE the block is skipped and the next statement, in our case printing 'done' is executed. If the condition of the while is TRUE, the block gets executed, and then the execution goes back to the condition again. It is evaluated again. If it is false the block is skipped and the 'done' is printed. If it is true the block gets executed and we are back to the condition ... This goes on as long as the condition is true or in sort-of English: while (the-condition-is-true) { do-something }
Infinite loopIn the above code we always decremented the variable so we knew that at one point the condition will become false. If for some reason the condition never becomes false you get an infinite loop. Your program will be stuck in a very small block it can never escape. For example if we had forgotten to decrement the $counter, or if we were incrementing it while still checking for a lower boundary. If this happens by mistake, that's a bug. In some cases using an infinite loop on purpose can make our program more simple to write and easier to read and we love readable code. If we would like to have an infinite loop we can use a condition that's always true. So we can write:
while (42) {
# here we do something
}
Of course people lacking the proper cultural reference will wonder why 42 so the more generally accepted, albeit slightly boring way is to always use the number 1 in infinite loops.
while (1) {
# here we do something
}
Naturally, seeing that the code has no escape from this loop, one will wonder how can that program ever end without killing it from the outside? For that, there are a number of ways.
One of them is executing the
use strict;
use warnings;
use 5.010;
while (1) {
print "Which programming language are you learning now? ";
my $name = <STDIN>;
chomp $name;
if ($name eq 'Perl') {
last;
}
say 'Wrong! Try again!';
}
say 'done';
In this example we ask the user a question and hope he will be able to answer it using the correct case. He will be stuck with this question forever if he cannot type 'Perl'. So the conversation might go on like this: Which programming language are you learning now? > Java Wrong! Try again! Which programming language are you learning now? > PHP Wrong! Try again! Which programming language are you learning now? > Perl done As you can see once the user typed the correct answer, last was called, the rest of the block including say 'Wrong! Try again!'; was skipped and the execution continued after the while loop.
ExerciseEarlier we learned about int and rand and started to write The Number Guessing game. Let's continue that game and implement the following: Let the user guess several times (with responses each time) till he finds the hidden number. Provide several control-keys: s - show the hidden value, the one the user needs to guess (cheat) (but this is also good for debugging) q - quit the game
Exercise 2Further improve the game by allowing the user to play several sets. So once the user finds out what was the number, generate a new number and start the game again. Also offer the following new control-keys: n - skip this game and start a new one (generate new number to guess) (aka "give up") d - debug mode This should be a toggle. At first the game does not show the hidden value. Pressing d once the system starts to show the hidden number every time before asking the user for new input. Pressing d again turns off the behavior. When you write a solution feel free to post it on your blog and link back to this page.
Perl tutorial and video courseFor further articles see the Beginner Perl Maven tutorial book and video course.In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation. blog comments powered by Disqus |
Follow me: