📄 ch4.htm
字号:
<TR><TD WIDTH=175><CENTER><TT>op1</TT>++</CENTER></TD><TD WIDTH=217>Post-iNCrement operand by one</TD></TR><TR><TD WIDTH=175><CENTER><TT>op1</TT>--</CENTER></TD><TD WIDTH=217>Post-decrement operand by one</TD></TR></TABLE></CENTER><P><P>Arithmetic operators start to get complicated when unary operatorsare introduced. Just between you and me, I didn't get the hangof negative numbers until someone said: "If you have fivepieces of chocolate, and add negative two pieces…"<P>You might think that adding negative numbers is strange. Not so.I know that you will never write a mathematics statement suchas the following: <TT>345 + -23</TT>.However, you might use <TT>354 + $gasBill</TT>,where <TT>$gasBill</TT> representsa 23-dollar debit-in other words, a negative number.<P>Using the unary plus operator does nothing, and Perl ignores it.The unary negative operator, however, changes the meaning of avalue from positive to negative or vice versa. For instaNCe, ifyou had a variable called <TT>$firstVar</TT>equal to 34, then printing <TT>-$firstVar</TT>would display -34.<P>The <TT>++</TT> and <TT>--</TT>operators are examples of the Perl shorthand notation. If the<TT>++</TT> or <TT>--</TT>operators appear in front of the operand, the operand is iNCrementedor decremented before its value is used. If the <TT>++</TT>or <TT>--</TT> operators appear afterthe operand, then the value of the operand is used and then theoperand is iNCremented or decremented as required.<H3><A NAME="ExampleThePreiNCrementOperator">Example: The Pre-iNCrement Operator</A></H3><P>This example shows how to use the pre-iNCrement operator (<I><B>++</B></I>).<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>The </I><TT><I>$numPages</I></TT><I>variable is assigned a value of 5.<BR>The </I><TT><I>$numPages</I></TT><I>variable is iNCremented by 1.<BR>The </I><TT><I>$numPages</I></TT><I>variable is printed.<BR>The </I><TT><I>$numPages</I></TT><I>variable is assigned a value of 5.<BR>The </I><TT><I>$numPages</I></TT><I>variables are iNCremented using the pre-iNCrement operator andthen printed.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 4.2 04LST02.PL-Using Pre-iNCrement Operator<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE># Original Way$numPages = 5;$numPages = $numPages + 1;print($numPages, "\n");# New Way$numPages = 5;print(++$numPages, "\n");</PRE></BLOCKQUOTE><HR><P>This program produces the following output:<BLOCKQUOTE><PRE>66</PRE></BLOCKQUOTE><P>You can see that the new way of coding is shorter than the originalway. The statement <TT>print(++$numPages,"\n");</TT> will first iNCrement the <TT>$numPages</TT>variable and then allow the print command to use it.<H3><A NAME="ExampleThePredecrementOperator">Example: The Pre-decrement Operator</A></H3><P>This example shows how to use the pre-decrement operator (<TT>--</TT>).<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>The </I><TT><I>$numPages</I></TT><I>variable is assigned a value of 5.<BR>The </I><TT><I>$numPages</I></TT><I>variable is decremented by 1.<BR>The </I><TT><I>$totalPages</I></TT><I>variable is assigned the value of </I><TT><I>$numPages+ 5</I></TT><I>.<BR>The </I><TT><I>$numPages</I></TT><I>and </I><TT><I>$totalPages</I></TT><I>variables are printed.<BR>The </I><TT><I>$numPages</I></TT><I>variable is assigned a value of 5.<BR>The </I><TT><I>$numPages</I></TT><I>variable is decremented and then </I><TT><I>$numPages+ 5</I></TT><I> is assigned to </I><TT><I>$totalPages</I></TT><I>.<BR>The </I><TT><I>$numPages</I></TT><I>and </I><TT><I>$totalPages</I></TT><I>variables are printed.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 4.3 04LST03.PL-Using Pre-iNCrement Operator<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE># Original Way$numPages = 5;$numPages = $numPages - 1;$totalPages = $numPages + 5;print("$numPages $totalPages \n");# New Way$numPages = 5;$totalPages = --$numPages + 5;print("$numPages $totalPages \n");</PRE></BLOCKQUOTE><HR><P>This program produces the following output:<BLOCKQUOTE><PRE>4 94 9</PRE></BLOCKQUOTE><P>The statement <TT>$totalPages = --$numPages+ 5;</TT> will first decrement the <TT>$numPages</TT>variable and then allow the plus operator to use it.<H3><A NAME="ExampleThePostiNCrementOperator">Example: The Post-iNCrement Operator</A></H3><P>This example shows how to use the ++ and -- post-iNCrement operators.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>The </I><TT><I>$numPages</I></TT><I>variable is assigned a value of 5.<BR>The </I><TT><I>$totalPages</I></TT><I>variable is assigned the value of </I><TT><I>$numPages</I></TT><I>.<BR>The </I><TT><I>$numPages</I></TT><I>variable is iNCremented by one.<BR>The </I><TT><I>$numPages</I></TT><I>and </I><TT><I>$totalPages</I></TT><I>variables are printed.<BR>The </I><TT><I>$numPages</I></TT><I>variable is assigned a value of 5.<BR>The </I><TT><I>$totalPages</I></TT><I>variable is assigned the value of </I><TT><I>$numPages</I></TT><I>and then the </I><TT><I>$numPages</I></TT><I>variable is iNCremented.<BR>The </I><TT><I>$numPages</I></TT><I>and </I><TT><I>$totalPages</I></TT><I>variables are printed.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 4.4 04LST04.PL-Using Pre-iNCrement Operator<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE># Original Way$numPages = 5;$totalPages = $numPages;$numPages = $numPages + 1;print("$numPages $totalPages \n");# New Way$numPages = 5;$totalPages = $numPages++;print("$numPages $totalPages \n");</PRE></BLOCKQUOTE><HR><P>The program produces the following output:<BLOCKQUOTE><PRE>6 56 5</PRE></BLOCKQUOTE><P>The statement <TT>$totalPages = $numPages++;</TT>will first assign the value of <TT>$numPages</TT>to <TT>$totalPages</TT> and then iNCrementthe <TT>$numPages</TT> variable. Itmay help to know that post-iNCrement and post-decrement operatorsdo not affect the value of the variable on the left side of theassignment operator. If you see post-iNCrement or post-decrementoperators, evaluate the statement by ignoring them. Then, whendone, apply the post-iNCrement and post-decrement operators asneeded.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>The Perl programming language has many ways of achieving the same objective. You will become a more efficient programmer if you decide on one approach to iNCrementing/decrementing and use it consistently.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="TheLogicalOperators"><FONT SIZE=5 COLOR=#FF0000>The Logical Operators</FONT></A></H2><P><I>Logical operators </I>are mainly used to control program flow.Usually, you will find them as part of an <TT>if</TT>,a <TT>while</TT>, or some other controlstatement. Control statements are discussed in <A HREF="ch7.htm" >Chapter 7</A> "ControlStatements."<BR><P><CENTER><B>Table 4.4 The Logical Operators</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=127><CENTER><I>Operator</I></CENTER></TD><TD WIDTH=336><I>Description</I></TD></TR><TR><TD WIDTH=127><CENTER><TT>op1 && op2</TT></CENTER></TD><TD WIDTH=336>Performs a logical AND of the two operands.</TD></TR><TR><TD WIDTH=127><CENTER><TT>op1 </TT>||<TT> op2</TT></CENTER></TD><TD WIDTH=336>Performs a logical OR of the two operands.</TD></TR><TR><TD WIDTH=127><CENTER><TT>!op1</TT></CENTER></TD><TD WIDTH=336>Performs a logical NOT of the operand.</TD></TR></TABLE></CENTER><P><P>The coNCept of logical operators is simple. They allow a programto make a decision based on multiple conditions. Each operandis considered a condition that can be evaluated to a true or falsevalue. Then the value of the conditions is used to determine theoverall value of the <TT>op1 operator op2</TT>or <TT>!op1</TT> grouping. The followingexamples demonstrate different ways that logical conditions canbe used.<H3><A NAME="ExampleThequotANDquotOperatorampamp">Example: The "AND" Operator (&&)</A></H3><P>The <TT>&&</TT> operator isused to determine whether both operands or conditions are true.Table 4.5 shows the results of using the <TT>&&</TT>operator on the four sets of true/false values.<BR><P><CENTER><B>Table 4.5 The && Result Table</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=67><CENTER><I>Op1</I></CENTER></TD><TD WIDTH=70><CENTER><I>Op2</I></CENTER></TD><TD WIDTH=140><CENTER><I>Op1 && Op2</I></CENTER></TD></TR><TR><TD WIDTH=67><CENTER>0</CENTER></TD><TD WIDTH=70><CENTER>0</CENTER></TD><TD WIDTH=140><CENTER>0</CENTER></TD></TR><TR><TD WIDTH=67><CENTER>1</CENTER></TD><TD WIDTH=70><CENTER>0</CENTER></TD><TD WIDTH=140><CENTER>0</CENTER></TD></TR><TR><TD WIDTH=67><CENTER>0</CENTER></TD><TD WIDTH=70><CENTER>1</CENTER></TD><TD WIDTH=140><CENTER>0</CENTER></TD></TR><TR><TD WIDTH=67><CENTER>1</CENTER></TD><TD WIDTH=70><CENTER>1</CENTER></TD><TD WIDTH=140><CENTER>1</CENTER></TD></TR></TABLE></CENTER><P><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>If the value of </I><TT><I>$firstVar</I></TT><I>is 10 AND the value of </I><TT><I>$secondVar</I></TT><I>is 9, then print the error message.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>if ($firstVar == 10 && $secondVar == 9) { print("Error!");};</PRE></BLOCKQUOTE><P>If either of the two conditions is false or iNCorrect, then theprint command is bypassed.<H3><A NAME="ExampleThequotORquotOperator">Example: The "OR" Operator (||)</A></H3><P>The || operator is used to determine whether either of the conditionsis true. Table 4.6 shows the results of using the || operatoron the four sets of true/false values.<BR><P><CENTER><B>Table 4.6 The || Result Table</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=73><CENTER><I>0p1 </I></CENTER></TD><TD WIDTH=94><CENTER><I>0p2 </I></CENTER></TD><TD WIDTH=104><CENTER><I>0p1 || 0p2</I></CENTER></TD></TR><TR><TD WIDTH=73><CENTER>0</CENTER></TD><TD WIDTH=94><CENTER>0</CENTER></TD><TD WIDTH=104><CENTER>0</CENTER></TD></TR><TR><TD WIDTH=73><CENTER>1</CENTER></TD><TD WIDTH=94><CENTER>0</CENTER></TD><TD WIDTH=104><CENTER>1</CENTER></TD></TR><TR><TD WIDTH=73><CENTER>0</CENTER></TD><TD WIDTH=94><CENTER>1</CENTER></TD><TD WIDTH=104><CENTER>1</CENTER></TD></TR><TR><TD WIDTH=73><CENTER>1</CENTER></TD><TD WIDTH=94><CENTER>1</CENTER></TD><TD WIDTH=104><CENTER>1</CENTER></TD></TR></TABLE></CENTER><P><P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>If the value of </I><TT><I>$firstVar</I></TT><I>is 9 OR the value of </I><TT><I>$firstVar</I></TT><I>is 10, then print the error message.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>if ($firstVar == 9 </FONT><FONT SIZE=2 FACE="Palatino">||</FONT><FONT SIZE=2 FACE="Courier"> $firstVar == 10) { print("Error!");</PRE></BLOCKQUOTE><P>If either of the two conditions is true, then the print commandis run.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></TD></TR><TR><TD><BLOCKQUOTE>If the first operand of the || operator evaluates to true, the second operand will not be evaluated. This could be a source of bugs if you are not careful. For instaNCe, in the following code fragment:</BLOCKQUOTE><BLOCKQUOTE><TT>if ($firstVar++ || $secondVar++) { print("\n"); }</TT></BLOCKQUOTE><BLOCKQUOTE>variable <TT>$secondVar</TT> will not be iNCremented if <TT>$firstVar++</TT> evaluates to true.</BLOCKQUOTE></TD></TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -