📄 ch8.htm
字号:
<TR VALIGN=TOP><TD WIDTH=169>3 + 4 <= 10</TD><TD WIDTH=126><CENTER>true</CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=169>3 + 4 == 4 + 4</TD><TD WIDTH=126><CENTER>false</CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=169>3 + 4 > 10</TD><TD WIDTH=126><CENTER>false</CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=169>3 + 4 >= 7</TD><TD WIDTH=126><CENTER>true</CENTER></TD></TR><TR VALIGN=TOP><TD WIDTH=169>3 + 4 >= 8</TD><TD WIDTH=126><CENTER>false</CENTER></TD></TR></TABLE></CENTER><P><H2><A NAME="LogicalOperators"><FONT SIZE=5 COLOR=#Ff0000>Logical Operators</FONT></A></H2><P>The comparison operators enable you to compare two expressions.But another type of operator-logical operators-supercharges comparisonoperators so that you can combine two or more logical expressionsinto a more complex logical expression. Even if you've never programmeda computer before, you're already familiar with logical operatorsbecause you use them in everyday speech. For example, when yousay, "Do you have a credit card or ten dollars in cash?"you're using the logical operator OR. Similarly, when you say,"I have a dog and a cat," you're using the AND operator.Table 8.3 lists Java's logical operators and what they mean.<BR><P><CENTER><B>Table 8.3 Java's Logical Operators.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=157><CENTER><I><B>Operator</B></I></CENTER></TD><TD WIDTH=144><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=157><CENTER>&&</CENTER></TD><TD WIDTH=144>AND</TD></TR><TR VALIGN=TOP><TD WIDTH=157><CENTER>||</CENTER></TD><TD WIDTH=144>OR</TD></TR><TR VALIGN=TOP><TD WIDTH=157><CENTER>^</CENTER></TD><TD WIDTH=144>Exclusive OR</TD></TR><TR VALIGN=TOP><TD WIDTH=157><CENTER>!</CENTER></TD><TD WIDTH=144>NOT</TD></TR></TABLE></CENTER><P><P>The AND (<TT>&&</TT>) operator requires all expressionsto be true for the entire expression to be true. For example,the expression<BLOCKQUOTE><PRE>(3 + 2 == 5) && (6 + 2 == 8)</PRE></BLOCKQUOTE><P>is true because the expressions on both sides of the <TT>&&</TT>are true. However, the expression<BLOCKQUOTE><PRE>(4 + 3 == 9) && (3 + 3 == 6)</PRE></BLOCKQUOTE><P>is false because the expression on the left of the <TT>&&</TT>is not true. Remember this when combining expressions with AND:If any expression is false, the entire expression is false.<P>The OR operator (<TT>||</TT>) requires only one expression tobe true for the entire expression to be true. For example, theexpressions<BLOCKQUOTE><PRE>(3 + 6 == 2) || (4 + 4 == 8)</PRE></BLOCKQUOTE><P>and<BLOCKQUOTE><PRE>(4 + 1 == 5) || (7 + 2 == 9)</PRE></BLOCKQUOTE><P>are both true because at least one of the expressions being comparedis true. Notice that in the second case both expressions beingcompared are true, which also makes an OR expression true.<P>The exclusive OR operator (^) is used to determine if one andonly one of the expressions being compared is true. Unlike a regularOR, with an exclusive OR, if both expressions are true, the resultis false (weird, huh?). For example, the expression<BLOCKQUOTE><PRE>(5 + 7 == 12) ^ (4 + 3 == 8)</PRE></BLOCKQUOTE><P>evaluates to true, whereas these expressions evaluate to false:<BLOCKQUOTE><PRE>(5 + 7 == 12) ^ (4 + 3 == 7)(5 + 7 == 10) ^ (4 + 3 == 6)</PRE></BLOCKQUOTE><P>The NOT (<TT>!</TT>) operator switches the value of (or negates)a logical expression. For example, the expression<BLOCKQUOTE><PRE>(4 + 3 == 5)</PRE></BLOCKQUOTE><P>is false; however, the expression<BLOCKQUOTE><PRE>!(4 + 3 == 5)</PRE></BLOCKQUOTE><P>is true.<H3><A NAME="ExampleUsingLogicalOperators">Example: Using Logical Operators</A></H3><P>Take a look at the following expression:<BLOCKQUOTE><PRE>(4 + 5 == 9) && !(3 + 1 = 3)</PRE></BLOCKQUOTE><P>Is this expression true or false? If you said true, you understandthe way the logical operators work. The expressions on eitherside of the && are both true, so the entire expressionis true. If you said false, you must go to bed without any dinner.<H3><A NAME="ExampleUsingMultipleLogicalOperators">Example: Using Multiple Logical Operators</A></H3><P>Just as with mathematical operators, you can use multiple logicaloperators to compare several logical expressions. For example,look at this expression:<BLOCKQUOTE><PRE>(4 == 4) && (5 == 5) && (6 == 6)</PRE></BLOCKQUOTE><P>This expression gives a result of <TT>true</TT> because each expressionto the left and right of each AND operator is true. However, thisexpression yields a value of false:<BLOCKQUOTE><PRE>(4 == 4) && (5 == 6) && (6 == 6)</PRE></BLOCKQUOTE><P>Remember that, when using AND, if any sub-expression is false,the entire expression is false. This is kind of like testifyingin court. To be true, it's got to be the truth, the whole truth,and nothing but the truth.<H3><A NAME="ExampleCombiningDifferentComparisonandLogicalOperators">Example: Combining Different Comparison and Logical Operators</A></H3><P>Again, just like mathematical operators, there's no restrictionon how you can combine the different comparison and logical operators,although if you build a very complex expression, you may havetrouble evaluating it yourself. Check out this expression:<BLOCKQUOTE><PRE>(3 < 5) && (2 == 2) && (9 > 6)</PRE></BLOCKQUOTE><P>Here you've used four different comparison and logical operatorsin the same complex expression. But because you're comparing thesub-expressions with the AND operator, and because each of thesub-expressions is true, the result of the above expression istrue.<P>Now, look at this expression:<BLOCKQUOTE><PRE>((3 < 5) && (2 == 1)) || (7 == 7)</PRE></BLOCKQUOTE><P>Yep, things are getting tricky. Is the above expression true orfalse? (Hey, give it a shot. You've got a fifty-fifty chance.)Ready for the answer? The above expression is true. First, lookat the parentheses. The outermost parentheses, on the left, groupthe two expressions being compared by the AND operator into asingle expression, so evaluate it first. The value 3 is less than5, but 2 does not equal 1, so the entire expression on the leftof the OR operator is false. On the right of the OR operator,however, 7 does indeed equal 7, so this sub-expression is true.Because one of the expressions in the OR comparison is true, theentire expression is true. Here's how the expression breaks down,step-by-step:<BLOCKQUOTE><PRE>((3 < 5) && (2 == 1)) || (7 == 7)((true) && (false)) || (7 == 7)false || (7 == 7)false || truetrue</PRE></BLOCKQUOTE><H2><A NAME="WritingLogicalExpressions"><FONT SIZE=5 COLOR=#Ff0000>Writing Logical Expressions</FONT></A></H2><P>You wouldn't write expressions such as<BLOCKQUOTE><PRE>(4 + 5 == 9) && !(3 + 1 == 3)</PRE></BLOCKQUOTE><P>in your programs. They would serve no purpose because you alreadyknow how the expressions evaluate. However, when you use variables,you have no way of knowing in advance how an expression may evaluate.For example, is the expression<BLOCKQUOTE><PRE>(num < 9) && (num > 15)</PRE></BLOCKQUOTE><P>true or false? You don't know without being told the value ofthe numerical variable <TT>num</TT>. By using logical operators,though, your program can do the evaluation, and, based on theresult-<TT>true</TT> or <TT>false</TT>-take the appropriate action.In the next chapter, which is about <TT>if</TT> and <TT>switch</TT>statements, you'll see how your programs can use logical expressionsto make decisions.<H2><A NAME="OrderofOperations"><FONT SIZE=5 COLOR=#Ff0000>Order of Operations</FONT></A></H2><P>Like all operators, comparison and logical operators have an orderof operations, or operator precedence. When you evaluate a complexexpression, you must be sure to evaluate any sub-expressions inthe correct order. As you learned in the previous example, however,you can use parentheses to group expressions so that they're easierto understand or to change the order of operations. Table 8.4lists the comparison and logical operators in order of precedence.<BR><P><CENTER><B>Table 8.4 Comparison and Logical Operators'Order of Operations.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=139><I><B>Operators</B></I></TD><TD WIDTH=210><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=139>!</TD><TD WIDTH=210>NOT</TD></TR><TR VALIGN=TOP><TD WIDTH=139>< > <= >=</TD><TD WIDTH=210>Relational</TD></TR><TR VALIGN=TOP><TD WIDTH=139>== !=</TD><TD WIDTH=210>Equality</TD></TR><TR VALIGN=TOP><TD WIDTH=139>^</TD><TD WIDTH=210>Exclusive OR</TD></TR><TR VALIGN=TOP><TD WIDTH=139>&&</TD><TD WIDTH=210>Logical AND</TD></TR><TR VALIGN=TOP><TD WIDTH=139>||</TD><TD WIDTH=210>Logical OR</TD></TR></TABLE></CENTER><P><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>Expressions, which are lines of Java code that can be reducedto a value or that assign a value, come in several types. In thischapter, you not only experimented with numerical and assignmentexpressions, but you also learned about logical expressions, whichyou create using the comparison and logical operators. Now thatyou know how to use comparison and logical operators to buildlogical expressions, you're ready to discover how computers makedecisions. You'll make that discovery in the next chapter, whereyou'll also start using the operators you learned in order towrite actual applets. Before you turn to that chapter, however,test your knowledge of expressions, comparison operators, andlogical operators by answering the following review questionsand by completing the review exercises.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>What is an expression?<LI>What are the three types of expressions?<LI>What is the result of the logical expression <TT>(3 < 5)</TT>?<LI>What is the result of the logical expression <TT>(3 < 5)&& (5 == 4 + 1)</TT>?<LI>Explain why expressions are recursive in nature.<LI>What are the six comparison operators?<LI>What are the four logical operators?<LI>What is the result of the logical expression <TT>(3 < 5)|| (6 == 5) || (3 != 3)</TT>?<LI>What's the result of the logical expression <TT>(5 != 10)&& ((3 == 2 + 1) ||(4 < 2 + 5))</TT>?<LI>What's the result of the logical expression <TT>!(5 == 2 +3) && !(5 + 2 !=7 - 5)</TT>?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Write an expression that compares three numbers for equality.<LI>Write an expression that determines whether one number isless than or equal to another.<LI>Write an expression that uses three different types of comparisonoperators and two different types of logical operators. The expressionmust be false.<LI>Suppose you have three variables, called <TT>num1</TT>, <TT>num2</TT>,and <TT>num3</TT>, in a program. Write an expression that comparesthe variables for equality.<LI>If the variable <TT>num1</TT> is equal to 5 and the variable<TT>num2</TT> is equal to 10, how would you evaluate the logicalexpression <TT>((num1 != 5) || (num2 == 10)) && !(num1== 5)</T<!-- reference library footer #1--></CENTER><IMG SRC="/images/rule.gif" WIDTH="460" HEIGHT="5" VSPACE="5"ALT="Ruler image"><br><FONT SIZE="-1">Contact <a href="mailto:reference@developer.com">reference@developer.com</a> with questions or comments.<br><a href="/legal/">Copyright 1998</a> <a href="http://www.earthweb.com" target="_top">EarthWeb Inc.</a>, All rights reserved.<BR>PLEASE READ THE <a href="/reference/usage.html">ACCEPTABLE USAGE STATEMENT</a>.<BR>Copyright 1998 Macmillan Computer Publishing. All rights reserved.</FONT></BLOCKQUOTE><!--outer table--><TD VALIGN="TOP"><!--right side ads --><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD1.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD1.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD2.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD2.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P></td></tr></table></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -