📄 ch04.htm
字号:
are equal by using the relational "equals" operator:</P>
<PRE><FONT COLOR="#0066FF">myAge == yourAge; // is the value in myAge the same as in yourAge?
</FONT></PRE>
<P>This expression evaluates to <TT>0</TT>, or <TT>false</TT>, because the variables
are not equal. The expression</P>
<PRE><FONT COLOR="#0066FF">myAge > yourAge; // is myAge greater than yourAge?
</FONT></PRE>
<P>evaluates to <TT>0</TT> or <TT>false</TT>.
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>WARNING:</B></FONT><B> </B>Many novice C++ programmers confuse
the assignment operator (<TT>=</TT>) with the equals operator (<TT>==</TT>). This
can create a nasty bug in your program.
<HR>
</BLOCKQUOTE>
<P>There are six relational operators: equals (<TT>==</TT>), less than (<TT><</TT>),
greater than (<TT>></TT>), less than or equal to (<TT><=</TT>), greater than
or equal to (<TT>>=</TT>), and not equals (<TT>!=</TT>). Table 4.1 shows each
relational operator, its use, and a sample code use.<BR>
<BR>
<FONT SIZE="4"><B>Table 4.1. The Relational Operators. </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><B><I>Name</I></B></TD>
<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 appears
in 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 usually
contains 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 a
single 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 only
is 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 demonstration
of branching based on relational operators</B></FONT><FONT SIZE="2" COLOR="#000077"><B>.</B></FONT><FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">1: // Listing 4.4 - demonstrates if statement
2: // used with relational operators
3: #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: 10
Enter the score for the Yankees: 10
A tie? Naah, can't be
Give me the real score for the Yanks: 8
Knew it! Go Sox!
Thanks for telling me.
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>This program asks for user
input of scores for two baseball teams, which are stored in integer variables. The
variables 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 the
scores are equal, the block of code that begins on line 24 and ends on line 38 is
entered. 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 would
not be invoked. The test on line 18 would evaluate as <TT>true</TT>, and the statements
on lines 20 and 21 would be invoked. Then the <TT>if</TT> statement on line 24 would
be tested, and this would be false (if line 18 was true). Thus, the program would
skip 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 stop
other <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 more
likely to create a religious war, however, than to ask a group of programmers what
is the best style for brace alignment. Although there are dozens of variations, these
appear 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 understand
where blocks of statements begin and end if the braces line up with each other and
with 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, another
if 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>, and
another 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, works
fine but is a bit cumbersome. The keyword <TT>else</TT> can make for far more readable
code:</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -