📄 ch7.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 7 -- Control Statements</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT SIZE=6 COLOR=#FF0000>Chapter 7</FONT></H1>
<H1><FONT SIZE=6 COLOR=#FF0000>Control Statements</FONT></H1>
<HR>
<P>
<CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER>
<UL>
<LI><A HREF="#DecisionStatements">
Decision Statements</A>
<UL>
<LI><A HREF="#ExampleTheifStatement">
Example: The if Statement</A>
</UL>
<LI><A HREF="#LoopStatements">
Loop Statements</A>
<UL>
<LI><A HREF="#ExampleIWhileILoops">
Example: <I>While</I> Loops</A>
<LI><A HREF="#ExampleIUntilILoops">
Example: <I>Until</I> Loops</A>
<LI><A HREF="#ExampleIForILoops">
Example: <I>For</I> Loops</A>
<LI><A HREF="#ExampleIForeachILoops">
Example: <I>Foreach</I> Loops</A>
</UL>
<LI><A HREF="#JumpKeywords">
Jump Keywords</A>
<UL>
<LI><A HREF="#ExampleTheIlastIKeyword">
Example: The <I>last</I> Keyword</A>
<LI><A HREF="#ExampleTheInextIKeyword">
Example: The <I>next</I> Keyword</A>
<LI><A HREF="#ExampleTheIredoIKeyword">
Example: The <I>redo</I> Keyword</A>
<LI><A HREF="#ExampleTheIgotoIKeyword">
Example: The <I>goto</I> Keyword</A>
</UL>
<LI><A HREF="#Summary">
Summary</A>
<LI><A HREF="#ReviewQuestions">
Review Questions</A>
<LI><A HREF="#ReviewExercises">
Review Exercises</A>
</UL>
<HR>
<P>
The last chapter, "Statements," discussed no-action,
action, and modified statements. This chapter discusses three
more types of statements: decision statements, loop statements,
and jump statements.
<P>
You see how to use the <TT>if</TT>
statement to decide on one or more courses of actions. Loop statements
are used to repeat a series of statements until a given condition
is either true or false. And finally, we'll wrap up the chapter
by looking at jump statements, which let you control program flow
by moving directly to the beginning or the end of a statement
block.
<H2><A NAME="DecisionStatements"><FONT SIZE=5 COLOR=#FF0000>
Decision Statements</FONT></A></H2>
<P>
<I>Decision statements </I>use the <I>if </I>keyword to execute
a statement block based on the evaluation of an expression or
to choose between executing one of two statement blocks based
on the evaluation of an expression. They are used quite often.
For example, a program might need to run one code section if a
customer is female and another code section if the customer is
male.
<H3><A NAME="ExampleTheifStatement">
Example: The if Statement</A></H3>
<P>
The syntax for the <TT>if</TT> statement
is the following:
<BLOCKQUOTE>
<PRE>
if (CONDITION) {
# Code block executed
# if condition is true.
} else {
# Code block executed
# if condition is false.
}
</PRE>
</BLOCKQUOTE>
<P>
Sometimes you need to choose from multiple statement blocks, such
as when you need to execute a different statement block for each
month. You use the <TT>if...elsif</TT>
statement for this type of decision. The <TT>if...elsif</TT>
statement has this syntax:
<BLOCKQUOTE>
<PRE>
if (CONDITION_ONE) {
# Code block executed
# if condition one is true.
} elsif (CONDITION_TWO) {
# Code block executed
# if condition two is true.
} else {
# Code block executed
# if all other conditions are false.
}
</PRE>
</BLOCKQUOTE>
<P>
Conditional expressions can use any of the operators discussed
in <A HREF="ch4.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch4.htm" >Chapter 4</A> "Operators." Even assignment operators
can be used because the value of an assignment expression is the
value that is being assigned. That last senteNCe may be a bit
confusing, so let's look at an example.
<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>Assign </I><TT><I>$firstVar</I></TT><I>
a value of 10.<BR>
Subtract five from </I><TT><I>$firstVar</I></TT><I>
and if the resulting value is true (for instaNCe, not zero), then
execute the statement block.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$firstVar = 10;
if ($firstVar -= 5) {
print("firstVar = $firstVar\n");
}
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
firstVar = 5<BR>
</PRE>
</BLOCKQUOTE>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If you're a C or C++ programmer, take heed: The curly braces around the statement block are <I>not</I> optional in Perl. Even one-line statement blocks must be surrounded by curly braces.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
This example, in addition to demonstrating the use of assignment
operators inside conditional expressions, also shows that the
<TT>else</TT> part of the <TT>if</TT>
statement is optional. If the <TT>else</TT>
part was coded, then it would only be executed when $firstVar
starts out with a value of 5.
<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>Assign </I><TT><I>$firstVar</I></TT><I>
a value of 10.<BR>
Subtract five from </I><TT><I>$firstVar</I></TT><I>
and if the resulting value is true (in other words, not zero),
then print </I><TT><I>$firstVar</I></TT><I>.
If not, print "</I><TT><I>firstVar is
zero</I></TT><I>."</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$firstVar = 5;
if ($firstVar -= 5) {
print("firstVar = $firstVar\n");
} else {
print("firstVar is zero\n");
}
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
firstVar is zero
</PRE>
</BLOCKQUOTE>
<P>
This example shows the use of the <TT>else</TT>
clause of the <TT>if</TT> statement.
Because the value of <TT>$firstVar</TT>
minus 5 was zero, the statements in the <TT>else</TT>
clause were executed.
<P>
You also can use the <TT>if</TT> statement
to select among multiple statement blocks. The <TT>if...elsif</TT>
form of the statement is used for this purpose.
<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>$month</I></TT><I>
to 2.<BR>
If the value of </I><TT><I>$month</I></TT><I>
is 1, then print January.<BR>
If the value of </I><TT><I>$month</I></TT><I>
is 2, then print February.<BR>
If the value of </I><TT><I>$month</I></TT><I>
is 3, then print March.<BR>
For every other value of </I><TT><I>$month</I></TT><I>,
print a message.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$month = 2;
if ($month == 1) {
print("January\n");
}
elsif ($month == 2) {
print("February\n");
}
elsif ($month == 3) {
print("March\n");
}
else {
print("Not one of the first three months\n");
}
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
February
</PRE>
</BLOCKQUOTE>
<P>
The <TT>else</TT> clause at the end
of the <TT>elsif</TT> chain serves
to catch any unknown or unforeseen values and is a good place
to put error messages. Frequently, those error messages should
iNClude the errant value and be written to a log file so that
the errors can be evaluated. After evaluation, you can decide
if the program needs to be modified to handle that unforeseen
value using another <TT>elsif</TT>
clause.
<H2><A NAME="LoopStatements"><FONT SIZE=5 COLOR=#FF0000>
Loop Statements</FONT></A></H2>
<P>
A loop is used to repeat the execution of a statement block until
a certain condition is reached. A loop can be used to iterate
through an array looking for a value. Loops also can be used
to count quantities. Actually, the number of uses for loops is
pretty much unlimited. There are three types of loops: while loops,
until loops, and for loops.
<H3><A NAME="ExampleIWhileILoops">
Example: <I>While</I> Loops</A></H3>
<P>
<TT><I>While</I></TT><I> </I>loops
are used to repeat a block of statements while some condition
is true. There are two forms of the loop: one where the condition
is checked before the statements are executed (the <TT>do..while</TT>
loop), and one in which the condition is checked after the statements
are executed (the <TT>while</TT> loop).
<P>
The <TT>do...while</TT> loop has this
syntax:
<BLOCKQUOTE>
<PRE>
do {
STATEMENTS
} while (CONDITION);
The while loop has this syntax:
while (CONDITION) {
STATEMENTS
}
continue {
STATEMENTS
}
</PRE>
</BLOCKQUOTE>
<P>
The statements in the <TT>continue</TT>
block of the <TT>while</TT> loop are
executed just before the loop starts the next iteration. The <TT>continue</TT>
block rarely is used. However, you can see it demonstrated in
the section, "Example: Using the <TT>-n</TT>
and <TT>-p</TT> Options," in
<A HREF="ch17.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch17.htm" >Chapter 17</A>, "Using Command-Line Options."
<P>
Which type you use for any particular task is entirely dependent
on your needs at the time. The statement block of a <TT>do...while</TT>
loop always will be executed at least oNCe. This is because the
condition is checked after the statement block is executed rather
than before. Here is an example of the <TT>do...while</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...while</I></TT><I>
loop.<BR>
Print the value of $firstVar.<BR>
INCrement $firstVar.<BR>
Check the </I><TT><I>while</I></TT><I>
condition; if true, 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++;
} while ($firstVar < 2);
print("outside: firstVar = $firstVar\n");
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
inside: firstVar = 10
outside: firstVar = 11
</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. This ability occasionally comes
in handy while counting down-such as when printing pages of a
report.
<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>$numPages</I></TT><I>
to 10.<BR>
Start the </I><TT><I>do...while</I></TT><I>
loop.<BR>
Print a page.<BR>
Decrement </I><TT><I>$numPages</I></TT><I>
and then loop if the condition is still true.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$numPages = 10;
do {
printPage();
} while (--$numPages);
</PRE>
</BLOCKQUOTE>
<P>
When this loop is done, all of the pages will have been displayed.
This type of loop would be used when you know that there always
will be pages to process. Notice that because the predecrement
operator is used, the <TT>$numPages</TT>
variable is decremented before the condition expression is evaluated.
<P>
If you need to ensure that the statement block does not get executed,
then you need to use the <TT>while</TT>
statement.
<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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -