⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch04.htm

📁 vc的电子书
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<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. Demonstrating
the else keyword.</B></FONT><FONT SIZE="2" COLOR="#000077"><B></B></FONT>
<PRE><FONT COLOR="#0066FF">1:   // Listing 4.5 - demonstrates if statement
2:   // with else clause
3:   #include &lt;iostream.h&gt;
4:   int main()
5:   {
6:      int firstNumber, secondNumber;
7:      cout &lt;&lt; &quot;Please enter a big number: &quot;;
8:      cin &gt;&gt; firstNumber;
9:      cout &lt;&lt; &quot;\nPlease enter a smaller number: &quot;;
10:     cin &gt;&gt; secondNumber;
11:     if (firstNumber &gt; secondNumber)
12:          cout &lt;&lt; &quot;\nThanks!\n&quot;;
13:     else
14:          cout &lt;&lt; &quot;\nOops. The second is bigger!&quot;;
15:
16:        return 0;
<TT>17: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Please enter a big number: 10

Please enter a smaller number: 12

Oops. The second is bigger!
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>The <TT>if</TT> statement
on 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 line
13 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 of
code 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>
<PRE><FONT COLOR="#0066FF">if (expression)
    statement;
next statement;
</FONT></PRE>
<P>If the expression is evaluated as <TT>TRUE</TT>, the statement is executed and
the program continues with the next statement. If the expression is not true, the
statement is ignored and the program jumps to the next statement. Remember that the
statement can be a single statement ending with a semicolon or a block enclosed in
braces. Form 2</P>
<PRE><FONT COLOR="#0066FF">if (expression)
    statement1;
else
    statement2;
next statement;
</FONT></PRE>
<P>If the expression evaluates <TT>TRUE</TT>, <TT>statement1</TT> is executed; otherwise,
<TT>statement2</TT> is executed. Afterwards, the program continues with the next
statement. Example 1</P>
<PRE><FONT COLOR="#0066FF">Example
if (SomeValue &lt; 10)
  cout &lt;&lt; &quot;SomeValue is less than 10&quot;);
else
  cout &lt;&lt; &quot;SomeValue is not less than 10!&quot;);
cout &lt;&lt; &quot;Done.&quot; &lt;&lt; endl;
</FONT></PRE>
<CENTER>
<H4><A NAME="Heading32"></A><FONT COLOR="#000077">Advanced if Statements</FONT></H4>
</CENTER>
<P>It is worth noting that any statement can be used in an <TT>if</TT> or <TT>else</TT>
clause, even another <TT>if</TT> or <TT>else</TT> statement. Thus, you might see
complex <TT>if</TT> statements in the following form:</P>
<PRE><FONT COLOR="#0066FF">if (expression1)
{
    if (expression2)
        statement1;
    else
    {
        if (expression3)
            statement2;
        else
            statement3;
    }
}
else
    statement4;
</FONT></PRE>
<P>This cumbersome <TT>if</TT> statement says, &quot;If expression1 is true and expression2
is true, execute statement1. If expression1 is true but expression2 is not true,
then if expression3 is true execute statement2. If expression1 is true but expression2
and expression3 are false, execute statement3. Finally, if expression1 is not true,
execute statement4.&quot; As you can see, complex <TT>if</TT> statements can be confusing!</P>
<P>Listing 4.6 gives an example of such a complex <TT>if</TT> statement.</P>

<P><A NAME="Heading33"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 4.6. A complex,
nested if statement.</B></FONT><FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">1:  // Listing 4.5 - a complex nested
2:  // if statement
3:  #include &lt;iostream.h&gt;
4:  int main()
5:  {
6:      // Ask for two numbers
7:      // Assign the numbers to bigNumber and littleNumber
8:      // If bigNumber is bigger than littleNumber,
9:      // see if they are evenly divisible
10:     // If they are, see if they are the same number
11:
12:     int firstNumber, secondNumber;
13:     cout &lt;&lt; &quot;Enter two numbers.\nFirst: &quot;;
14:     cin &gt;&gt; firstNumber;
15:     cout &lt;&lt; &quot;\nSecond: &quot;;
16:     cin &gt;&gt; secondNumber;
17:     cout &lt;&lt; &quot;\n\n&quot;;
18:
19:     if (firstNumber &gt;= secondNumber)
20:     {
21:       if ( (firstNumber % secondNumber) == 0) // evenly divisible?
22:       {
23:            if (firstNumber == secondNumber)
24:                 cout &lt;&lt; &quot;They are the same!\n&quot;;
25:            else
26:                 cout &lt;&lt; &quot;They are evenly divisible!\n&quot;;
27:       }
28:       else
29:            cout &lt;&lt; &quot;They are not evenly divisible!\n&quot;;
30:     }
31:     else
32:       cout &lt;&lt; &quot;Hey! The second one is larger!\n&quot;;
33:        return 0;
<TT>34: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Enter two numbers.
First: 10

Second: 2

They are evenly divisible!
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Two numbers are prompted
for one at a time, and then compared. The first <TT>if</TT> statement, on line 19,
checks to ensure that the first number is greater than or equal to the second. If
not, the <TT>else</TT> clause on line 31 is executed.<BR>
If the first <TT>if</TT> is true, the block of code beginning on line 20 is executed,
and the second <TT>if</TT> statement is tested, on line 21. This checks to see whether
the first number modulo the second number yields no remainder. If so, the numbers
are either evenly divisible or equal. The <TT>if</TT> statement on line 23 checks
for equality and displays the appropriate message either way.</P>
<P>If the <TT>if</TT> statement on line 21 fails, the <TT>else</TT> statement on
line 28 is executed.
<CENTER>
<H3><A NAME="Heading35"></A><FONT COLOR="#000077">Using Braces in Nested if Statements</FONT></H3>
</CENTER>
<P>Although it is legal to leave out the braces on <TT>if</TT> statements that are
only a single statement, and it is legal to nest <TT>if</TT> statements, such as</P>
<PRE><FONT COLOR="#0066FF">if (x &gt; y)              // if x is bigger than y
    if (x &lt; z)          // and if x is smaller than z
        x = y;         // then set x to the value in z
</FONT></PRE>
<P>when writing large nested statements, this can cause enormous confusion. Remember,
whitespace and indentation are a convenience for the programmer; they make no difference
to the compiler. It is easy to confuse the logic and inadvertently assign an <TT>else</TT>
statement to the wrong <TT>if</TT> statement. Listing 4.7 illustrates this problem.</P>

<P><A NAME="Heading36"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 4.7. A demonstration
of why braces help clarify which else statement goes with which if statement.</B></FONT><FONT
COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">1:   // Listing 4.7 - demonstrates why braces
2:   // are important in nested if statements
3:   #include &lt;iostream.h&gt;
4:   int main()
5:   {
6:     int x;
7:     cout &lt;&lt; &quot;Enter a number less than 10 or greater than 100: &quot;;
8:     cin &gt;&gt; x;
9:     cout &lt;&lt; &quot;\n&quot;;
10:
11:     if (x &gt; 10)
12:        if (x &gt; 100)
13:             cout &lt;&lt; &quot;More than 100, Thanks!\n&quot;;
14:     else                            // not the else intended!
15:        cout &lt;&lt; &quot;Less than 10, Thanks!\n&quot;;
16:
17:         return 0; 
<TT>18: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Enter a number less than 10 or greater than 100: 20

Less than 10, Thanks!
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>The programmer intended to ask for
a number between 10 and 100, check for the correct value, and then print a thank-you
note.<BR>
If the <TT>if</TT> statement on line 11 evaluates <TT>TRUE</TT>, the following statement
(line 12) is executed. In this case, line 12 executes when the number entered is
greater than 10. Line 12 contains an <TT>if</TT> statement also. This <TT>if</TT>
statement evaluates <TT>TRUE</TT> if the number entered is greater than 100. If the
number is not greater than 100, the statement on line 13 is executed.</P>
<P>If the number entered is less than or equal to 10, the <TT>if</TT> statement on
line 10 evaluates to <TT>FALSE</TT>. Program control goes to the next line following
the <TT>if</TT> statement, in this case line 16. If you enter a number less than
10, the output is as follows:</P>
<PRE><FONT COLOR="#0066FF">Enter a number less than 10 or greater than 100: 9</FONT></PRE>
<P>The <TT>else</TT> clause on line 14 was clearly intended to be attached to the
<TT>if</TT> statement on line 11, and thus is indented accordingly. Unfortunately,
the <TT>else</TT> statement is really attached to the <TT>if</TT> statement on line
12, and thus this program has a subtle bug.</P>

<P>It is a subtle bug because the compiler will not complain. This is a legal C++
program, but it just doesn't do what was intended. Further, most of the times the
programmer tests this program, it will appear to work. As long as a number that is
greater than 100 is entered, the program will seem to work just fine. <BR>
<BR>
Listing 4.8 fixes the problem by putting in the necessary braces.</P>

<P><A NAME="Heading38"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 4.8. A demonstration
of the proper use of braces with an if statement</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1:    // Listing 4.8 - demonstrates proper use of braces
2:    // in nested if statements
3:    #include &lt;iostream.h&gt;
4:    int main()
5:    {
6:      int x;
7:      cout &lt;&lt; &quot;Enter a number less than 10 or greater than 100: &quot;;
8:      cin &gt;&gt; x;
9:      cout &lt;&lt; &quot;\n&quot;;
10:
11:     if (x &gt; 10)
12:     {
13:        if (x &gt; 100)
14:             cout &lt;&lt; &quot;More than 100, Thanks!\n&quot;;
15:     }
16:     else                            // not the else intended!
17:        cout &lt;&lt; &quot;Less than 10, Thanks!\n&quot;;
18:         return 0;
<TT>19: }</TT>
Output: Enter a number less than 10 or greater than 100: 20
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>The braces on lines 12 and 15 make
everything between them into one statement, and now the <TT>else</TT> on line 16
applies to the <TT>if</TT> on line 11 as intended.<BR>
The user typed <TT>20</TT>, so the <TT>if</TT> statement on line 11 is true; however,
the <TT>if</TT> statement on line 13 is false, so nothing is printed. It would be
better if the programmer put another <TT>else</TT> clause after line 14 so that errors
would be caught and a message printed.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>The programs shown in this book
	are written to demonstrate the particular issues being discussed. They are kept intentionally
	simple; there is no attempt to &quot;bulletproof&quot; the code to protect against
	user error. In professional-quality code, every possible user error is anticipated
	and handled gracefully. 
<HR>


</BLOCKQUOTE>

<CENTER>
<H3><A NAME="Heading40"></A><FONT COLOR="#000077">Logical Operators</FONT></H3>
</CENTER>
<P>Often you want to ask more than one relational question at a time. &quot;Is it
true that x is greater than y, and also true that y is greater than z?&quot; A program
might need to determine that both of these conditions are true, or that some other
condition is true, in order to take an action.</P>
<P>Imagine a sophisticated alarm system that has this logic: &quot;If the door alarm
sounds AND it is after six p.m. AND it is NOT a holiday, OR if it is a weekend, then
call the police.&quot; C++'s three logical operators are used to make this kind of
evaluation. These operators are listed in Table 4.2.<BR>
<BR>
<FONT SIZE="4"><B>Table 4.2. The Logical Operators. </B></FONT>
<TABLE BORDER="0">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT"><B><I>Operator</I></B></TD>
		<TD ALIGN="LEFT"><B><I>Symbol</I></B></TD>
		<TD ALIGN="LEFT"><B><I>Example</I></B></TD>
	</TR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -