📄 ch04.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Teach Yourself C++ in 21 Days</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1 ALIGN="CENTER"><A HREF="ch03.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch03.htm"><IMG SRC="BLANPREV.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANPREV.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="tppmsgs/msgs0.htm#1" tppabs="http://www.mcp.com/sams"><IMG
SRC="BLANHOME.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
BORDER="0"></A><A HREF="index.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/index.htm"><IMG SRC="BLANTOC.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANTOC.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch05.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch05.htm"><IMG SRC="BLANNEXT.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANNEXT.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A></H1>
<H1></H1>
<UL>
<LI><A HREF="#Heading1">Day 4</A>
<UL>
<LI><A HREF="#Heading2">Expressions and Statements</A>
<UL>
<LI><A HREF="#Heading3">Statements</A>
<UL>
<LI><A HREF="#Heading4">Whitespace</A>
<LI><A HREF="#Heading5">Blocks and Compound Statements</A>
</UL>
<LI><A HREF="#Heading6">Expressions</A>
<LI><A HREF="#Heading7">Listing 4.1. Evaluating complex expressions</A><A HREF="#Heading8">.</A>
<LI><A HREF="#Heading9">Operators</A>
<UL>
<LI><A HREF="#Heading10">Assignment Operator</A>
<LI><A HREF="#Heading11">Mathematical Operators</A>
</UL>
<LI><A HREF="#Heading12">Listing 4.2. A demonstration of subtraction and integer
overflow</A><A HREF="#Heading13">.</A>
<UL>
<LI><A HREF="#Heading14">Integer Division and Modulus</A>
</UL>
<LI><A HREF="#Heading15">Combining the Assignment and Mathematical Operators</A>
<LI><A HREF="#Heading16">Increment and Decrement</A>
<UL>
<LI><A HREF="#Heading17">Prefix and Postfix</A>
</UL>
<LI><A HREF="#Heading18">Listing 4.3. A demonstration of prefix and postfix operators</A><A
HREF="#Heading19">.</A>
<LI><A HREF="#Heading20">Precedence</A>
<LI><A HREF="#Heading21">Nesting Parentheses</A>
<LI><A HREF="#Heading22">The Nature of Truth</A>
<UL>
<LI><A HREF="#Heading23">Relational Operators</A>
</UL>
<LI><A HREF="#Heading24">The if Statement</A>
<LI><A HREF="#Heading25">Listing 4.4. A demonstration of branching based on relational
operators</A><A HREF="#Heading26">.</A>
<UL>
<LI><A HREF="#Heading27">Indentation Styles</A>
<LI><A HREF="#Heading28">else</A>
</UL>
<LI><A HREF="#Heading29">Listing 4.5. Demonstrating the else keyword</A><A HREF="#Heading30">.</A>
<LI><A HREF="#Heading31">The if Statement</A>
<UL>
<LI><A HREF="#Heading32">Advanced if Statements</A>
</UL>
<LI><A HREF="#Heading33">Listing 4.6. A complex, nested if statement</A><A HREF="#Heading34">.</A>
<LI><A HREF="#Heading35">Using Braces in Nested if Statements</A>
<LI><A HREF="#Heading36">Listing 4.7. A demonstration of why</A>
<LI><A HREF="#Heading37">braces help clarify which else statement goes with which
if statement.</A>
<LI><A HREF="#Heading38">Listing 4.8. A demonstration of the proper use of braces
with an if statement</A><A HREF="#Heading39">.</A>
<LI><A HREF="#Heading40">Logical Operators</A>
<UL>
<LI><A HREF="#Heading41">Logical AND</A>
<LI><A HREF="#Heading42">Logical OR</A>
<LI><A HREF="#Heading43">Logical NOT</A>
</UL>
<LI><A HREF="#Heading44">Relational Precedence</A>
<LI><A HREF="#Heading45">More About Truth and Falsehood</A>
<LI><A HREF="#Heading46">Conditional (Ternary) Operator</A>
<LI><A HREF="#Heading47">Listing 4.9. A demonstration of the conditional operator</A><A
HREF="#Heading48">.</A>
<LI><A HREF="#Heading49">Summary</A>
<LI><A HREF="#Heading50">Q&A</A>
<LI><A HREF="#Heading51">Workshop</A>
<UL>
<LI><A HREF="#Heading52">Quiz</A>
<LI><A HREF="#Heading53">Exercises</A>
</UL>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 4</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Expressions and Statements</FONT></H2>
<P>At its heart, a program is a set of commands executed in sequence. The power in
a program comes from its capability to execute one or another set of commands, based
on whether a particular condition is true or false. Today you will learn
<UL>
<LI>What statements are.
<P>
<LI>What blocks are.
<P>
<LI>What expressions are.
<P>
<LI>How to branch your code based on conditions.
<P>
<LI>What truth is, and how to act on it.
</UL>
<H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">Statements</FONT></H3>
<P>In C++ a statement controls the sequence of execution, evaluates an expression,
or does nothing (the null statement). All C++ statements end with a semicolon, even
the null statement, which is just the semicolon and nothing else. One of the most
common statements is the following assignment statement:</P>
<PRE><FONT COLOR="#0066FF">x = a + b;
</FONT></PRE>
<P>Unlike in algebra, this statement does not mean that <TT>x</TT> equals <TT>a+b</TT>.
This is read, "Assign the value of the sum of <TT>a</TT> and <TT>b</TT> to <TT>x</TT>,"
or "Assign to <TT>x</TT>, <TT>a+b</TT>." Even though this statement is
doing two things, it is one statement and thus has one semicolon. The assignment
operator assigns whatever is on the right side of the equal sign to whatever is on
the left side.</P>
<DL>
<DD>
<HR>
<FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>null statement</I> is a
statement that does nothing.
<HR>
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading4"></A><FONT COLOR="#000077">Whitespace</FONT></H4>
<P>Whitespace (tabs, spaces, and newlines) is generally ignored in statements. The
assignment statement previously discussed could be written as</P>
<PRE><FONT COLOR="#0066FF">x=a+b;
</FONT></PRE>
<P>or as</P>
<PRE><FONT COLOR="#0066FF">x =a
+ b ;
</FONT></PRE>
<P>Although this last variation is perfectly legal, it is also perfectly foolish.
Whitespace can be used to make your programs more readable and easier to maintain,
or it can be used to create horrific and indecipherable code. In this, as in all
things, C++ provides the power; you supply the judgment.</P>
<DL>
<DD>
<HR>
<FONT COLOR="#000077"><B>New Term: </B></FONT><I>Whitespace characters</I> (spaces,
tabs, and newlines) cannot be seen. If these characters are printed, you see only
the white of the paper.
<HR>
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">Blocks and Compound
Statements</FONT></H4>
<P>Any place you can put a single statement, you can put a compound statement, also
called a block. A block begins with an opening brace (<TT>{</TT>) and ends with a
closing brace (<TT>}</TT>). Although every statement in the block must end with a
semicolon, the block itself does not end with a semicolon. For example</P>
<PRE><FONT COLOR="#0066FF">{
temp = a;
a = b;
b = temp;
}
</FONT></PRE>
<P>This block of code acts as one statement and swaps the values in the variables
<TT>a</TT> and <TT>b</TT>.
<BLOCKQUOTE>
<P>
<HR>
<B>DO</B> use a closing brace any time you have an opening brace.<B> DO</B> end your
statements with a semicolon. <B>DO</B> use whitespace judiciously to make your code
clearer.
<HR>
</BLOCKQUOTE>
<H3 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Expressions</FONT></H3>
<P>Anything that evaluates to a value is an expression in C++. An expression is said
to return a value. Thus, <TT>3+2;</TT> returns the value <TT>5</TT> and so is an
expression. All expressions are statements.</P>
<P>The myriad pieces of code that qualify as expressions might surprise you. Here
are three examples:</P>
<PRE><FONT COLOR="#0066FF">3.2 // returns the value 3.2
PI // float const that returns the value 3.14
SecondsPerMinute // int const that returns 60
</FONT></PRE>
<P>Assuming that <TT>PI</TT> is a constant equal to <TT>3.14</TT> and <TT>SecondsPerMinute</TT>
is a constant equal to <TT>60</TT>, all three of these statements are expressions.</P>
<P>The complicated expression</P>
<PRE><FONT COLOR="#0066FF">x = a + b;
</FONT></PRE>
<P>not only adds <TT>a</TT> and <TT>b</TT> and assigns the result to <TT>x</TT>,
but returns the value of that assignment (the value of <TT>x</TT>) as well. Thus,
this statement is also an expression. Because it is an expression, it can be on the
right side of an assignment operator:</P>
<PRE><FONT COLOR="#0066FF">y = x = a + b;
</FONT></PRE>
<P>This line is evaluated in the following order: Add <TT>a</TT> to <TT>b</TT>.</P>
<P>Assign the result of the expression <TT>a + b</TT> to <TT>x</TT>.<BR>
<BR>
Assign the result of the assignment expression <TT>x = a + b</TT> to <TT>y</TT>.<BR>
<BR>
If <TT>a</TT>, <TT>b</TT>, <TT>x</TT>, and <TT>y</TT> are all integers, and if <TT>a</TT>
has the value <TT>2</TT> and <TT>b</TT> has the value <TT>5</TT>, both <TT>x</TT>
and <TT>y</TT> will be assigned the value <TT>7</TT>.</P>
<P><A NAME="Heading7"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 4.1. Evaluating
complex expressions.</B></FONT><FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">1: #include <iostream.h>
2: int main()
3: {
4: int a=0, b=0, x=0, y=35;
5: cout << "a: " << a << " b: " << b;
6: cout << " x: " << x << " y: " << y << endl;
7: a = 9;
8: b = 7;
9: y = x = a+b;
10: cout << "a: " << a << " b: " << b;
11: cout << " x: " << x << " y: " << y << endl;
12: return 0;
<TT>13: }</TT>
Output: a: 0 b: 0 x: 0 y: 35
a: 9 b: 7 x: 16 y: 16
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>On line 4, the four variables are
declared and initialized. Their values are printed on lines 5 and 6. On line 7, <TT>a</TT>
is assigned the value <TT>9</TT>. One line 8, <TT>b</TT> is assigned the value <TT>7</TT>.
On line 9, the values of <TT>a</TT> and <TT>b</TT> are summed and the result is assigned
to <TT>x</TT>. This expression (<TT>x = a+b</TT>) evaluates to a value (the sum of
<TT>a + b</TT>), and that value is in turn assigned to <TT>y</TT>.
<H3 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">Operators</FONT></H3>
<P>An operator is a symbol that causes the compiler to take an action. Operators
act on operands, and in C++ all operands are expressions. In C++ there are several
different categories of operators. Two of these categories are
<UL>
<LI>Assignment operators.
<P>
<LI>Mathematical operators.
</UL>
<H4 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">Assignment Operator</FONT></H4>
<P>The assignment operator (<TT>=</TT>) causes the operand on the left side of the
assignment operator to have its value changed to the value on the right side of the
assignment operator. The expression</P>
<PRE><FONT COLOR="#0066FF">x = a + b;
</FONT></PRE>
<P>assigns the value that is the result of adding <TT>a</TT> and <TT>b</TT> to the
operand <TT>x</TT>.</P>
<P>An operand that legally can be on the left side of an assignment operator is called
an lvalue. That which can be on the right side is called (you guessed it) an rvalue.</P>
<P>Constants are r-values. They cannot be l-values. Thus, you can write</P>
<PRE><FONT COLOR="#0066FF">x = 35; // ok
</FONT></PRE>
<P>but you can't legally write</P>
<PRE><FONT COLOR="#0066FF">35 = x; // error, not an lvalue!
</FONT></PRE>
<DL>
<DD>
<HR>
<FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>An <I>lvalue</I> is an operand
that can be on the left side of an expression. An rvalue is an operand that can be
on the right side of an expression. Note that all l-values are r-values, but not
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -