📄 ch6.htm
字号:
used. Listing 6.1 shows the <TT>unless</TT>modifier used in a program.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Call the </I><TT><I>assignElement()</I></TT><I>fuNCtion to create two elements in the </I><TT><I>@array</I></TT><I>associative array.<BR>Call the </I><TT><I>printArray()</I></TT><I>fuNCtion.<BR>Try to redefine the value associated with the "A" keyby calling </I><TT><I>assignElement()</I></TT><I>.<BR>Print the array again to verify that no elements have changed.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 6.1 06LST01.PL-Using the <FONT FACE="BI Helvetica BoldOblique">unless</FONT>Modifier to Control Array Element Assignmen</B>t<BR></BLOCKQUOTE><BLOCKQUOTE><PRE>assignElement("A", "AAAA");assignElement("B", "BBBB");printArray();assignElement("A", "ZZZZ");printArray();sub assignElement { my($key, $value) = @_; $array{$key} = $value unless defined($array{$key});}sub printArray { while (($key, $value) = each(%array)) { print("$key = $value\n"); } print("\n");}</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>A = AAAAB = BBBBA = AAAAB = BBBB</PRE></BLOCKQUOTE><P>These lines of code should look a little familiar to you. The<TT>while</TT> loop in the <TT>printArray()</TT>fuNCtion was used in a <A HREF="ch5.htm" >Chapter 5</A>example. The <TT>assignElement()</TT>fuNCtion will make an assignment unless a key-value pair withthe same key already exists. In that case, the assignment statementis bypassed.<H3><A NAME="ExampleUsingtheuntilModifier">Example: Using the until Modifier</A></H3><P>The <TT>until</TT> modifier is a littlemore complex than the <TT>if</TT>or <TT>unless</TT> modifiers. It repeatedlyevaluates the expression until the condition becomes true. Thebasic syntax of a modified statement with the <TT>until</TT>modifier is<BLOCKQUOTE><PRE>EXPRESSION until (CONDITION);</PRE></BLOCKQUOTE><P>This is a compact way of saying<BLOCKQUOTE><PRE>until (CONDITION) { EXPRESSION;}</PRE></BLOCKQUOTE><P>The expression is evaluated only while the condition is false.If the condition is true when the statement is eNCountered, theexpression will never be evaluated. The following example provesthis:<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize </I><TT><I>$firstVar</I></TT><I>to 10.<BR>Repeatedly evaluate </I><TT><I>$firstVar++</I></TT><I>until the condition </I><TT><I>$firstVar> 2</I></TT><I> is true.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 10;$firstVar++ until ($firstVar > 2);print("firstVar = $firstVar\n");</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>firstVar = 10</PRE></BLOCKQUOTE><P>This shows that the expression <TT>$firstVar++</TT>was never executed because the condition was true the first timeit was evaluated. If it had been executed, the value of <TT>$firstVar</TT>would have been 11 when printed. In this case, the <TT>until</TT>modifier worked exactly like the <TT>unless</TT>modifier.<P>However, when the condition is false for the first evaluation,Perl executes the expression repeatedly until the condition istrue. Here is an example:<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize </I><TT><I>$firstVar</I></TT><I>to 10.<BR>Repeatedly evaluate </I><TT><I>$firstVar++</I></TT><I>until the condition </I><TT><I>$firstVar> 20</I></TT><I> is true.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 10;$firstVar++ until ($firstVar > 20);print("firstVar = $firstVar\n");</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>firstVar = 21</PRE></BLOCKQUOTE><P>In this case, the <TT>$firstVar++</TT>expression is executed 11 times. Each execution of the expressioniNCrements the value of <TT>$firstVar</TT>.When <TT>$firstVar</TT> is equal to21, the statement ends because 21 is greater than 20, which meansthat the condition is true.<P>You can find out about the <TT>until</TT>statement-as opposed to the <TT>until</TT>modifier-in <A HREF="ch7.htm" >Chapter 7</A> "Control Statements."<H3><A NAME="ExampleUsingthewhileModifier">Example: Using the while Modifier</A></H3><P>The <TT>while</TT> modifier is theopposite of the <TT>until</TT> modifier.It repeatedly evaluates the expression while the condition istrue. When the condition becomes false, the statement ends. Thebasic syntax of a modified statement with the <TT>while</TT>modifier is<BLOCKQUOTE><PRE>EXPRESSION while (CONDITION);</PRE></BLOCKQUOTE><P>This is a compact way of saying<BLOCKQUOTE><PRE>while (CONDITION) { EXPRESSION;}</PRE></BLOCKQUOTE><P>The expression is evaluated only while the condition is true.If the condition is false when the statement is eNCountered, theexpression will never be evaluated. Here is an example using the<TT>while</TT> modifier.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize </I><TT><I>$firstVar</I></TT><I>to 10.<BR>Repeatedly evaluate </I><TT><I>$firstVar++</I></TT><I>while the condition </I><TT><I>$firstVar< 20</I></TT><I> is true.<BR>Print the value of </I><TT><I>$firstVar</I></TT><I>.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 10;$firstVar++ while ($firstVar < 20);print("firstVar = $firstVar\n");</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>firstVar = 21</PRE></BLOCKQUOTE><P>You can compare this example directly to the last example givenfor the <TT>until</TT> modifier. Becausethe <TT>until</TT> modifier is theopposite of the <TT>while</TT> modifier,the operators in the conditions are also opposite in nature.<P>You can find out about the <TT>while</TT>statement-as opposed to the <TT>while</TT>modifier-in <A HREF="ch7.htm" >Chapter 7</A> "Control Statements."<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A></H2><P>This chapter discussed Perl statements and how they are builtfrom expressions. You read about four types of expressions: simple,simple with side effects, simple with operators, and complex.<P>Next, you read about statement blocks. These program constructsare good to logically isolate one block of statements from themain program flow. You can also use statement blocks and the <TT>my()</TT>fuNCtion to create local variables. This is mainly done for debuggingreasons or to make small program changes that are guaranteed notto affect other portions of the program.<P>Then, seven types of statements were mentioned: no-action, action,assignment, decision, jump, loop, and modified. This chapter describedno-action, action, and modified statements. Assignment statementswere mentioned in <A HREF="ch3.htm" >Chapter 3</A>"Variables" and again in<A HREF="ch4.htm" >Chapter 4</A> "Operators." Decision, jump, and loop statementsare covered in <A HREF="ch7.htm" >Chapter 7</A> "Control Statements."<P>Modified statements use the <TT>if</TT>,<TT>unless</TT>, <TT>until</TT>,and <TT>while</TT> keywords to affectthe evaluation of an expression. The <TT>if</TT>keyword evaluates an expression if a given condition is true.The <TT>unless</TT> keyword does theopposite: the expression is evaluated if a given condition isfalse. The <TT>until</TT> keywordrepeatedly evaluates an expression until the condition is true.The <TT>while</TT> keyword is theopposite of until so that it repeatedly evaluates an expressionuntil the condition is false.<P>The next chapter, "Control Statements," explores the<TT>decision</TT>, <TT>jump</TT>,and <TT>loop</TT> statements in detail.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#FF0000>Review Questions</FONT></A></H2><P>Answers to Review Questions are in Appendix A.<OL><LI>What is an expression?<LI>What is a statement?<LI>What are the four statement modifiers?<LI>What are two uses for statement blocks?<LI>What can non-action statements be used for?<LI>How is the <TT>if</TT> modifierdifferent from the <TT>unless</TT>modifier?<LI>What will the following code display?</OL><BLOCKQUOTE><PRE>$firstVar = 10;$secondVar = 20;$firstVar += $secondVar++ if ($firstVar > 10);print("firstVar = $firstVar\n");print("firstVar = $secondVar\n");</PRE></BLOCKQUOTE><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>Review Exercises</FONT></A></H2><OL><LI>Write a simple expression that uses the exponentiation operator.<LI>Write a complex expression that uses three operators and onefuNCtion.<LI>Write a Perl program that uses a statement block inside afuNCtion call.<LI>Use the statement block from the previous exercise to createlocal variables.<LI>Write a Perl program that shows if the expression clause ofa <TT>while</TT> modified statementwill be evaluated when the condition is false.</OL><HR><CENTER><P><A HREF="ch5.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch7.htm"><IMG SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><HR WIDTH="100%"></P></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -