forked from szabgab/perlmaven.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhile-loop.tt
More file actions
128 lines (97 loc) · 3.85 KB
/
while-loop.tt
File metadata and controls
128 lines (97 loc) · 3.85 KB
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
=title while loop
=timestamp 2013-03-12T12:45:51
=indexes while, while (1), loop, infinite loop, last
=status show
=books beginner_book
=author szabgab
=index 1
=archive 1
=feed 1
=comments 1
=social 1
=abstract start
In this episode of the <a href="/perl-tutorial">Perl tutorial</a> we are going to see <b>how does the while loop work in Perl</b>.
=abstract end
<code lang="perl">
use strict;
use warnings;
use 5.010;
my $counter = 10;
while ($counter > 0) {
say $counter;
$counter -= 2;
}
say 'done';
</code>
The <hl>while</hl> 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 beginning of the while loop it checks if the condition is <a href="/boolean-values-in-perl">true or
false</a>. If it is <hl>FALSE</hl> the block is skipped and the next statement, in our case printing 'done' is executed.
If the condition of the <hl>while</hl> is <hl>TRUE</hl>, 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:
<hl>while (the-condition-is-true) { do-something }</hl>
<h2>Infinite loop</h2>
In 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 <hl>infinite loop</hl>. Your program will be stuck in a
very small block it can never escape.
For example if we had forgotten to decrement the <hl>$counter</hl>, or if we were incrementing
it while still checking for a lower boundary.
If this happens by mistake, that's a bug.
On the other hand, in some cases using an infinite loop <b>on purpose</b> can make our program more simple to write, and
easier to read. 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:
<code lang="perl">
while (42) {
# here we do something
}
</code>
Of course people lacking the
<a href="http://en.wikipedia.org/wiki/Answer_to_Life,_the_Universe,_and_Everything#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe.2C_and_Everything_.2842.29">proper cultural reference</a>
will wonder why 42 so the more generally accepted, albeit slightly boring way is to always use the number 1 in infinite loops.
<code lang="perl">
while (1) {
# here we do something
}
</code>
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 calling the <hl>last</hl> statement inside the while loop.
That will skip the rest of the block and won't check the condition again.
Effectively ending the loop. People usually put it in some kind of a condition.
<code lang="perl">
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';
</code>
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:
<code>
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
</code>
As you can see once the user typed the correct answer, <hl>last</hl> was called, the rest of the block
including <hl>say 'Wrong! Try again!';</hl> was skipped and the execution continued after the
<hl>while loop</hl>.