📄 ch04.htm
字号:
<TD ALIGN="LEFT"><B><I>Operator</I></B></TD> <TD ALIGN="LEFT"><B><I>Sample</I></B></TD> <TD ALIGN="LEFT"><B><I>Evaluates</I></B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">Equals</TD> <TD ALIGN="LEFT"><TT>==</TT></TD> <TD ALIGN="LEFT"><TT>100 == 50;</TT></TD> <TD ALIGN="LEFT"><TT>false</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT"><TT>50 == 50;</TT></TD> <TD ALIGN="LEFT"><TT>true</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">Not Equals</TD> <TD ALIGN="LEFT"><TT>!=</TT></TD> <TD ALIGN="LEFT"><TT>100 != 50;</TT></TD> <TD ALIGN="LEFT"><TT>true</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT"><TT>50 != 50;</TT></TD> <TD ALIGN="LEFT"><TT>false</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">Greater Than</TD> <TD ALIGN="LEFT"><TT>></TT></TD> <TD ALIGN="LEFT"><TT>100 > 50;</TT></TD> <TD ALIGN="LEFT"><TT>true</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT"><TT>50 > 50;</TT></TD> <TD ALIGN="LEFT"><TT>false</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">Greater Than</TD> <TD ALIGN="LEFT"><TT>>=</TT></TD> <TD ALIGN="LEFT"><TT>100 >= 50;</TT></TD> <TD ALIGN="LEFT"><TT>true</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">or Equals</TD> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT"><TT>50 >= 50;</TT></TD> <TD ALIGN="LEFT"><TT>true</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">Less Than</TD> <TD ALIGN="LEFT"><TT><</TT></TD> <TD ALIGN="LEFT"><TT>100 < 50;</TT></TD> <TD ALIGN="LEFT"><TT>false</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT"><TT>50 < 50;</TT></TD> <TD ALIGN="LEFT"><TT>false</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">Less Than</TD> <TD ALIGN="LEFT"><TT><=</TT></TD> <TD ALIGN="LEFT"><TT>100 <= 50;</TT></TD> <TD ALIGN="LEFT"><TT>false</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT">or Equals</TD> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT"><TT>50 <= 50;</TT></TD> <TD ALIGN="LEFT"><TT>true</TT></TD> </TR></TABLE><BLOCKQUOTE> <P><HR><B>DO</B> remember that relational operators return the value <TT>1</TT> (<TT>true</TT>) or <TT>0</TT> (<TT>false</TT>). <B>DON'T</B> confuse the assignment operator (<TT>=</TT>) with the equals relational operator (<TT>==</TT>). This is one of the most common C++ programming mistakes--be on guard for it. <HR></BLOCKQUOTE><CENTER><H3><A NAME="Heading24"></A><FONT COLOR="#000077">The if Statement</FONT></H3></CENTER><P>Normally, your program flows along line by line in the order in which it appearsin your source code. The <TT>if</TT> statement enables you to test for a condition(such as whether two variables are equal) and branch to different parts of your code,depending on the result.</P><P>The simplest form of an <TT>if</TT> statement is this:</P><PRE><FONT COLOR="#0066FF">if (expression) statement;</FONT></PRE><P>The expression in the parentheses can be any expression at all, but it usuallycontains one of the relational expressions. If the expression has the value <TT>0</TT>,it is considered false, and the statement is skipped. If it has any nonzero value,it is considered true, and the statement is executed. Consider the following example:</P><PRE><FONT COLOR="#0066FF">if (bigNumber > smallNumber) bigNumber = smallNumber;</FONT></PRE><P>This code compares <TT>bigNumber</TT> and <TT>smallNumber</TT>. If <TT>bigNumber</TT>is larger, the second line sets its value to the value of <TT>smallNumber</TT>.</P><P>Because a block of statements surrounded by braces is exactly equivalent to asingle statement, the following type of branch can be quite large and powerful:</P><PRE><FONT COLOR="#0066FF">if (expression){ statement1; statement2; statement3;}</FONT></PRE><P>Here's a simple example of this usage:</P><PRE><FONT COLOR="#0066FF">if (bigNumber > smallNumber){ bigNumber = smallNumber; cout << "bigNumber: " << bigNumber << "\n"; cout << "smallNumber: " << smallNumber << "\n";}</FONT></PRE><P>This time, if <TT>bigNumber</TT> is larger than <TT>smallNumber</TT>, not onlyis it set to the value of <TT>smallNumber</TT>, but an informational message is printed.Listing 4.4 shows a more detailed example of branching based on relational operators.</P><P><A NAME="Heading25"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 4.4. A demonstrationof branching based on relational operators</B></FONT><FONT SIZE="2" COLOR="#000077"><B>.</B></FONT><FONTCOLOR="#0066FF"></FONT><PRE><FONT COLOR="#0066FF">1: // Listing 4.4 - demonstrates if statement2: // used with relational operators3: #include <iostream.h>4: int main()5: {6: int RedSoxScore, YankeesScore;7: cout << "Enter the score for the Red Sox: ";8: cin >> RedSoxScore;9:10: cout << "\nEnter the score for the Yankees: ";11: cin >> YankeesScore;12:13: cout << "\n";14:15: if (RedSoxScore > YankeesScore)16: cout << "Go Sox!\n";17:18: if (RedSoxScore < YankeesScore)19: {20: cout << "Go Yankees!\n";21: cout << "Happy days in New York!\n";22: }23:24: if (RedSoxScore == YankeesScore)25: {26: cout << "A tie? Naah, can't be.\n";27: cout << "Give me the real score for the Yanks: ";28: cin >> YankeesScore;29:30: if (RedSoxScore > YankeesScore)31: cout << "Knew it! Go Sox!";32:33: if (YankeesScore > RedSoxScore)34: cout << "Knew it! Go Yanks!";35:36: if (YankeesScore == RedSoxScore)37: cout << "Wow, it really was a tie!";38: }39:40: cout << "\nThanks for telling me.\n";41: return 0;<TT>42: }</TT>Output: Enter the score for the Red Sox: 10Enter the score for the Yankees: 10A tie? Naah, can't beGive me the real score for the Yanks: 8Knew it! Go Sox!Thanks for telling me.</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>This program asks for userinput of scores for two baseball teams, which are stored in integer variables. Thevariables are compared in the <TT>if</TT> statement on lines 15, 18, and 24.<BR>If one score is higher than the other, an informational message is printed. If thescores are equal, the block of code that begins on line 24 and ends on line 38 isentered. The second score is requested again, and then the scores are compared again.</P><P>Note that if the initial Yankees score was higher than the Red Sox score, the<TT>if</TT> statement on line 15 would evaluate as <TT>FALSE</TT>, and line 16 wouldnot be invoked. The test on line 18 would evaluate as <TT>true</TT>, and the statementson lines 20 and 21 would be invoked. Then the <TT>if</TT> statement on line 24 wouldbe tested, and this would be false (if line 18 was true). Thus, the program wouldskip the entire block, falling through to line 39.</P><P>In this example, getting a true result in one <TT>if</TT> statement does not stopother <TT>if</TT> statements from being tested.<CENTER><H4><A NAME="Heading27"></A><FONT COLOR="#000077">Indentation Styles</FONT></H4></CENTER><P>Listing 4.3 shows one style of indenting <TT>if</TT> statements. Nothing is morelikely to create a religious war, however, than to ask a group of programmers whatis the best style for brace alignment. Although there are dozens of variations, theseappear to be the favorite three:<UL> <LI>Putting the initial brace after the condition and aligning the closing brace under the <TT>if</TT> to close the statement block.</UL><PRE><FONT COLOR="#0066FF">if (expression){ statements}</FONT></PRE><UL> <LI>Aligning the braces under the <TT>if</TT> and indenting the statements.</UL><PRE><FONT COLOR="#0066FF">if (expression){ statements}</FONT></PRE><UL> <LI>Indenting the braces and statements.</UL><PRE><FONT COLOR="#0066FF">if (expression) { statements }</FONT></PRE><P>This book uses the middle alternative, because I find it easier to understandwhere blocks of statements begin and end if the braces line up with each other andwith the condition being tested. Again, it doesn't matter much which style you choose,as long as you are consistent with it.<CENTER><H4><A NAME="Heading28"></A><FONT COLOR="#000077">else</FONT></H4></CENTER><P>Often your program will want to take one branch if your condition is true, anotherif it is false. In Listing 4.3, you wanted to print one message (<TT>Go Sox!</TT>)if the first test (<TT>RedSoxScore > Yankees</TT>) evaluated <TT>TRUE</TT>, andanother message (<TT>Go Yanks!</TT>) if it evaluated <TT>FALSE</TT>.</P><P>The method shown so far, testing first one condition and then the other, worksfine but is a bit cumbersome. The keyword <TT>else</TT> can make for far more readablecode:</P><PRE><FONT COLOR="#0066FF">if (expression) statement;else statement;</FONT></PRE><P>Listing 4.5 demonstrates the use of the keyword <TT>else</TT>.</P><P><A NAME="Heading29"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 4.5. Demonstratingthe else keyword.</B></FONT><FONT SIZE="2" COLOR="#000077"><B></B></FONT><PRE><FONT COLOR="#0066FF">1: // Listing 4.5 - demonstrates if statement2: // with else clause3: #include <iostream.h>4: int main()5: {6: int firstNumber, secondNumber;7: cout << "Please enter a big number: ";8: cin >> firstNumber;9: cout << "\nPlease enter a smaller number: ";10: cin >> secondNumber;11: if (firstNumber > secondNumber)12: cout << "\nThanks!\n";13: else14: cout << "\nOops. The second is bigger!";15:16: return 0;<TT>17: }</TT></FONT><FONT COLOR="#0066FF">Output: Please enter a big number: 10Please enter a smaller number: 12Oops. The second is bigger!</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>The <TT>if</TT> statementon line 11 is evaluated. If the condition is true, the statement on line 12 is run;if it is false, the statement on line 14 is run. If the <TT>else</TT> clause on line13 were removed, the statement on line 14 would run whether or not the <TT>if</TT>statement was true. Remember, the <TT>if</TT> statement ends after line 12. If the<TT>else</TT> was not there, line 14 would just be the next line in the program.<BR>Remember that either or both of these statements could be replaced with a block ofcode in braces.<CENTER><H3><A NAME="Heading31"></A><FONT COLOR="#000077">The if Statement</FONT></H3></CENTER><P>The syntax for the <TT>if</TT> statement is as follows: Form 1</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -