📄 ch7.htm
字号:
Start the </I><TT><I>while</I></TT><I>
loop and test the condition. If false, don't execute the statement
block.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.
<BR>
INCrement </I><TT><I>$firstVar</I></TT><I>.
<BR>
Jump back to the start of the statement block and test the condition
again.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$firstVar = 10;
while ($firstVar < 2) {
print("inside: firstVar = $firstVar\n");
$firstVar++;
};
print("outside: firstVar = $firstVar\n");
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
outside: firstVar = 10
</PRE>
</BLOCKQUOTE>
<P>
This example shows that the statement block is never evaluated
if the condition is false when the <TT>while</TT>
loop starts. Of course, it's more common to use <TT>while</TT>
loops that actually execute the statement block-like the following:
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Initialize </I><TT><I>$firstVar</I></TT><I>
to 10.<BR>
Start the </I><TT><I>while</I></TT><I>
loop and test the condition.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.
<BR>
INCrement </I><TT><I>$firstVar</I></TT><I>.
<BR>
Jump back to the start of the statement block and test the condition
again.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$firstVar = 10;
while ($firstVar < 12) {
print("inside: firstVar = $firstVar\n");
$firstVar++;
};
print("outside: firstVar = $firstVar\n");
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
inside: firstVar = 10
inside: firstVar = 11
outside: firstVar = 12
</PRE>
</BLOCKQUOTE>
<P>
It's important to note that the value of <TT>$firstVar</TT>
ends up as 12 and not 11 as you might expect upon casually looking
at the code. When <TT>$firstVar</TT>
is still 11, the condition is true, so the statement block is
executed again, thereby iNCrementing <TT>$firstVar</TT>
to 12. Then, the next time the condition is evaluated, it is false
and the loop ends with <TT>$firstVar</TT>
equal to 12.
<H3><A NAME="ExampleIUntilILoops">
Example: <I>Until</I> Loops</A></H3>
<P>
<TT><I>Until</I></TT> loops are used
to repeat a block of statements while some condition is false.
Like the previous <TT>while</TT> loop,
there are also two forms of the <TT>until</TT>
loop: one where the condition is checked before the statements
are executed (the <TT>do...until</TT>
loop), and one in which the condition is checked after the statements
are executed (the <TT>until</TT> loop).
<P>
The <TT>do...until</TT> loop has this
syntax:
<BLOCKQUOTE>
<PRE>
do {
STATEMENTS
} until (CONDITION);
The until loop has this syntax:
until (CONDITION) {
STATEMENTS
}
</PRE>
</BLOCKQUOTE>
<P>
Again, the loop type you use is dependent on your needs at the
time. Here is an example of the <TT>do...until</TT>
loop.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Initialize </I><TT><I>$firstVar</I></TT><I>
to 10.<BR>
Start the </I><TT><I>do..until</I></TT><I>
loop.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.
<BR>
INCrement </I><TT><I>$firstVar</I></TT><I>.
<BR>
Check the </I><TT><I>until</I></TT><I>
condition; if false, jump back to the start of the statement block.
<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$firstVar = 10;
do {
print("inside: firstVar = $firstVar\n");
$firstVar++;
} until ($firstVar < 2);
print("outside: firstVar = $firstVar\n");
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
inside: firstVar = 10
inside: firstVar = 11
inside: firstVar = 12
inside: firstVar = 13
inside: firstVar = 14
...
</PRE>
</BLOCKQUOTE>
<P>
This loop continues forever because the condition can never be
true. <TT>$firstVar</TT> starts out
greater than 2 and is iNCremented inside the loop. Therefore,
this is an <I>endless </I>loop.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If you ever find it hard to understand a conditional expression in a loop statement, try the following: Wrap the entire condition expression inside paren-theses and add == 1 to the right-hand side. The above loop then becomes</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<BLOCKQUOTE>
<PRE>
do {
...
} until (($firstVar < 2) == 1);
</PRE>
</BLOCKQUOTE>
<P>
This example shows that the statement block is executed even though
the condition <TT>$firstVar < 2</TT>
is false when the loop starts. The next example shows the <TT>until</TT>
loop in action, which does not execute the statement block when
the conditional expression is false when the loop starts.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Initialize </I><TT><I>$firstVar</I></TT><I>
to 10.<BR>
Start the </I><TT><I>until</I></TT><I>
loop and test the condition. If true, don't execute the state-ment
block.<BR>
print the value of </I><TT><I>$firstVar</I></TT><I>.
<BR>
INCrement </I><TT><I>$firstVar</I></TT><I>.
<BR>
Jump back to the start of the statement block and test the condition
again.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$firstVar = 10;
until ($firstVar < 20) {
print("inside: firstVar = $firstVar\n");
$firstVar++;
};
print("outside: firstVar = $firstVar\n");
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
outside: firstVar = 10
</PRE>
</BLOCKQUOTE>
<P>
This example shows that the statement block is never evaluated
if the condition is true when the <TT>until</TT>
loop starts. Here is another example of an <TT>until</TT>
loop that shows the statement block getting executed:
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Initialize </I><TT><I>$firstVar</I></TT><I>
to 10.<BR>
Start the </I><TT><I>while</I></TT><I>
loop and test the condition.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.
<BR>
INCrement </I><TT><I>$firstVar</I></TT><I>.
<BR>
Jump back to the start of the statement block and test the condition
again.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$firstVar = 10;
until ($firstVar > 12) {
print("inside: firstVar = $firstVar\n");
$firstVar++;
};
print("outside: firstVar = $firstVar\n");
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
inside: firstVar = 10
inside: firstVar = 11
inside: firstVar = 12
outside: firstVar = 13
</PRE>
</BLOCKQUOTE>
<H3><A NAME="ExampleIForILoops">
Example: <I>For</I> Loops</A></H3>
<P>
One of the most common tasks in programming is looping a specific
number of times. Whether you need to execute a certain fuNCtion
for every customer in your database or print a page in a report,
the <TT><I>for</I></TT> loop can be
used. Its syntax is:
<BLOCKQUOTE>
<PRE>
for (INITIALIZATION; CONDITION; INCREMENT/DECREMENT) {
STATEMENTS
}
</PRE>
</BLOCKQUOTE>
<P>
The <I>initialization</I> expression is executed first-before
the looping starts. It can be used to initialize any variables
that are used inside the loop. Of course, this could be done on
the line before the <TT>for</TT> loop.
However, iNCluding the initialization inside the <TT>for</TT>
statement aids in identifying the loop variables.
<P>
When initializing variables, be sure not to confuse the equality
operator (<TT>==</TT>) with the assignment
operator (<TT>=</TT>). The following
is an example of what this error could look like:
<BLOCKQUOTE>
<PRE>
for ($index == 0; $index < 0; $index++)
</PRE>
</BLOCKQUOTE>
<P>
One of the equal signs should be removed. If you think you are
having a problem with programming the <TT>for</TT>
loop, make sure to check out the operators.
<P>
The <I>condition</I> expression is used to determine whether the
loop should continue or be ended. When the condition expression
evaluates to false, the loop will end.
<P>
The <I>iNCrement/decrement </I>expression is used to modify the
loop variables in some way each time the code block has been executed.
Here is an example of a basic <TT>for</TT>
loop:
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Start the for loop by initializing </I><TT><I>$firstVar</I></TT><I>
to zero. The </I><TT><I>$firstVar</I></TT><I>
variable will be iNCremented each time the statement block is
executed. The statement block will be executed as long as </I><TT><I>$firstVar</I></TT><I>
is less than 100.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>
each time through the loop.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
for ($firstVar = 0; $firstVar < 100; $firstVar++) {
print("inside: firstVar = $firstVar\n");
}
</PRE>
</BLOCKQUOTE>
<P>
This program will display:
<BLOCKQUOTE>
<PRE>
inside: firstVar = 0
inside: firstVar = 1
...
inside: firstVar = 98
inside: firstVar = 99
</PRE>
</BLOCKQUOTE>
<P>
This program will display the numbers 0 through 99. When the loop
is over, <TT>$firstVar</TT> will be
equal to 100.
<P>
<TT>For</TT> loops also can be used
to count backwards.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Start the </I><TT><I>for</I></TT><I>
loop by initializing </I><TT><I>$firstVar</I></TT><I>
to 100. The </I><TT><I>$firstVar</I></TT><I>
variable will be decremented each time the statement block is
executed. And the statement block will be executed as long as
</I><TT><I>$firstVar</I></TT><I> is
greater than 0.<BR>
Print the value of </I><TT><I>$firstVar</I></TT><I>
each time through the loop.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
for ($firstVar = 100; $firstVar > 0; $firstVar--) {
print("inside: firstVar = $firstVar\n");
}
</PRE>
</BLOCKQUOTE>
<P>
This program will display:
<BLOCKQUOTE>
<PRE>
inside: firstVar = 100
inside: firstVar = 99
...
inside: firstVar = 2
inside: firstVar = 1
</PRE>
</BLOCKQUOTE>
<P>
You can use the comma operator to evaluate two expressions at
oNCe in the initialization and the iNCrement/decrement expressions.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Start the </I><TT><I>for</I></TT><I>
loop by initializing </I><TT><I>$firstVar</I></TT><I>
to 100 and </I><TT><I>$secondVar</I></TT><I>
to 0. The </I><TT><I>$firstVar</I></TT><I>
variable will be decremented and </I><TT><I>$secondVar</I></TT><I>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -