📄 ch4.htm
字号:
</PRE>
</BLOCKQUOTE>
<P>
Notice that every tenth item is printed. By changing the value
on the right side of the modulus operator, you can affect how
many items are processed before the message is printed. Changing
the value to 15 means that a message will be printed every 15
items. <A HREF="ch7.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch7.htm" >Chapter 7</A> "Control Statements," describes the
<TT>if</TT> and <TT>for</TT>
statement in detail.
<H2><A NAME="TheUnaryArithmeticOperators"><FONT SIZE=5 COLOR=#FF0000>
The Unary Arithmetic Operators</FONT></A></H2>
<P>
The unary arithmetic operators act on a single operand. They are
used to change the sign of a value, to iNCrement a value, or to
decrement a value. <I>INCrementing</I> a value means to add one
to its value. <I>Decrementing</I> a value means to subtract one
from its value. Table 4.3 lists Perl's unary operators.<BR>
<P>
<CENTER><B>Table 4.3 The Unary Arithmetic Operators</B></CENTER>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD WIDTH=175><CENTER><I>Operator</I></CENTER></TD><TD WIDTH=217><I>Description</I>
</TD></TR>
<TR><TD COLSPAN=2 WIDTH=392><B>Changing the sign of op1</B></TD>
</TR>
<TR><TD WIDTH=175><CENTER>+<TT>op1</TT></CENTER>
</TD><TD WIDTH=217>Positive operand</TD></TR>
<TR><TD WIDTH=175><CENTER>-<TT>op1</TT></CENTER>
</TD><TD WIDTH=217>Negative operand</TD></TR>
<TR><TD COLSPAN=2 WIDTH=392><B>Changing the value of op1 before usage</B>
</TD></TR>
<TR><TD WIDTH=175><CENTER>++<TT>op1</TT></CENTER>
</TD><TD WIDTH=217>Pre-iNCrement operand by one</TD></TR>
<TR><TD WIDTH=175><CENTER>--<TT>op1</TT></CENTER>
</TD><TD WIDTH=217>Pre-decrement operand by one</TD></TR>
<TR><TD COLSPAN=2 WIDTH=392><B>Changing the value of op1 after usage</B>
</TD></TR>
<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 operators
are introduced. Just between you and me, I didn't get the hang
of negative numbers until someone said: "If you have five
pieces 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 such
as the following: <TT>345 + -23</TT>.
However, you might use <TT>354 + $gasBill</TT>,
where <TT>$gasBill</TT> represents
a 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 a
value from positive to negative or vice versa. For instaNCe, if
you 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 iNCremented
or decremented before its value is used. If the <TT>++</TT>
or <TT>--</TT> operators appear after
the operand, then the value of the operand is used and then the
operand 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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 and
then 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>
6
6
</PRE>
</BLOCKQUOTE>
<P>
You can see that the new way of coding is shorter than the original
way. 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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 9
4 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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 5
6 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 iNCrement
the <TT>$numPages</TT> variable. It
may help to know that post-iNCrement and post-decrement operators
do not affect the value of the variable on the left side of the
assignment operator. If you see post-iNCrement or post-decrement
operators, evaluate the statement by ignoring them. Then, when
done, apply the post-iNCrement and post-decrement operators as
needed.<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 control
statement. Control statements are discussed in <A HREF="ch7.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch7.htm" >Chapter 7</A> "Control
Statements."<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 program
to make a decision based on multiple conditions. Each operand
is considered a condition that can be evaluated to a true or false
value. Then the value of the conditions is used to determine the
overall value of the <TT>op1 operator op2</TT>
or <TT>!op1</TT> grouping. The following
examples demonstrate different ways that logical conditions can
be used.
<H3><A NAME="ExampleThequotANDquotOperatorampamp">
Example: The "AND" Operator (&&)</A></H3>
<P>
The <TT>&&</TT> operator is
used 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -