📄 http:^^www.cs.cornell.edu^info^courses^current^cs100b^rdz2.html
字号:
MIME-Version: 1.0
Server: CERN/3.0
Date: Sunday, 24-Nov-96 22:48:48 GMT
Content-Type: text/html
Content-Length: 17673
Last-Modified: Tuesday, 03-Sep-96 19:38:27 GMT
<HTML>
<HEAD>
<TITLE>tt3</TITLE>
<META NAME="GENERATOR" CONTENT="Internet Assistant for Microsoft Word 2.0z">
</HEAD>
<BODY>
<P>
<CENTER><B><FONT SIZE=6>CS100</FONT></B></CENTER>
<P>
<CENTER><B><FONT SIZE=6>Lecture <B>2<BR>
</B></FONT></B></CENTER>
<P>
<FONT SIZE=6>REVIEW ó previous lecture</FONT>
<P>
<FONT SIZE=6>problem, algorithm, program, language, computer,
program/data distinction, input/output, execution, trace<BR>
</FONT>
<P>
<FONT SIZE=6>program structure, output statement, conditional
expression, operators, input function<BR>
<BR>
</FONT>
<P>
<FONT SIZE=6>CONCEPTS ó this lecture</FONT>
<P>
<FONT SIZE=6>expressions: arithmetic, relational, logical, conditional;
operator precedence and parenthesization; function application;
convergence & divergence, strict & non-strict evaluation,
programs; output; libraries<BR>
</FONT>
<P>
<FONT SIZE=6>READING</FONT>
<P>
<FONT SIZE=6>Teitelbaum, Sections 2.1ñ2.7</FONT>
<P>
<FONT SIZE=6>Roberts, Section 2.1, Section 3.5</FONT>
<H1><CENTER><FONT SIZE=6>Expressions<BR>
</FONT></CENTER></H1>
<P>
<FONT SIZE=6>ï Familiar from grade school arithmetic.<BR>
</FONT>
<P>
<FONT SIZE=6>(2 </FONT><FONT SIZE=6 FACE="Symbol">¥</FONT><FONT SIZE=6>
3) </FONT><FONT SIZE=6 FACE="Symbol">+</FONT><FONT SIZE=6> (20
</FONT><FONT SIZE=6 FACE="Symbol">_</FONT><FONT SIZE=6> 5)<BR>
</FONT>
<P>
<FONT SIZE=6>ï Consist of <I>operators</I>, e.g. </FONT><FONT SIZE=6 FACE="Symbol">+</FONT><FONT SIZE=6>,</FONT><FONT SIZE=6 FACE="Symbol">
¥,</FONT><FONT SIZE=6> </FONT><FONT SIZE=6 FACE="Symbol">_</FONT><FONT SIZE=6>,
and <I>operands</I>, e.g., <I>constants</I>. An operator applied
to its operands is an <I>application</I>. Sub-expressions are
parenthesized to indicate structure.<BR>
</FONT>
<P>
<FONT SIZE=6>ï Reduce to a single <I>value</I> by process
of <I>evaluation</I>.<BR>
</FONT>
<P>
<FONT SIZE=6>(2 </FONT><FONT SIZE=6 FACE="Symbol">¥</FONT><FONT SIZE=6>
3) + (20 </FONT><FONT SIZE=6 FACE="Symbol">_</FONT><FONT SIZE=6>
5) </FONT><FONT SIZE=6 FACE="Symbol">Þ</FONT><FONT SIZE=6>
10<BR>
</FONT>
<P>
<FONT SIZE=6>ï Evaluation of an expression can be <I>traced</I>.
<BR>
</FONT>
<P>
<U><FONT SIZE=6>(2 </FONT><FONT SIZE=6 FACE="Symbol">¥</FONT><FONT SIZE=6>
3)</FONT></U><FONT SIZE=6> + (20 </FONT><FONT SIZE=6 FACE="Symbol">_</FONT><FONT SIZE=6>
5)</FONT>
<P>
<FONT SIZE=6>6 + <U>(20 </U></FONT><U><FONT SIZE=6 FACE="Symbol">_</FONT><FONT SIZE=6>
5)</FONT></U>
<P>
<U><FONT SIZE=6>6 + 4</FONT></U>
<P>
<FONT SIZE=6>10<BR>
<BR>
<BR>
</FONT>
<P>
<FONT SIZE=6>The process used in evaluation and tracing is expression
simplification.<BR>
</FONT>
<HR>
<P>
<B><FONT SIZE=6>Inside-Out Simplification:</FONT></B><FONT SIZE=6>
To evaluate expression <I>e</I>, repeatedly replace an innermost
application <I>e'</I> within <I>e</I> by its value using the appropriate
rule of arithmetic. Stop when <I>e</I> has been reduced to a single
value.</FONT>
<HR>
<P>
<FONT SIZE=6>Note: left-to-right evaluation not required.</FONT>
<H1><CENTER><FONT SIZE=6>Expressions in C ó Operations
<BR>
</FONT></CENTER></H1>
<P>
<FONT SIZE=6>Binary Operators<BR>
</FONT>
<TABLE BORDER=1>
<TR><TD WIDTH=187><FONT SIZE=6>Operator</FONT></TD><TD WIDTH=60><FONT SIZE=5 FACE="Monaco">+</FONT>
</TD><TD WIDTH=60><FONT SIZE=5 FACE="Monaco">-</FONT></TD><TD WIDTH=60><FONT SIZE=5 FACE="Monaco">*</FONT>
</TD><TD WIDTH=60><FONT SIZE=5 FACE="Monaco">/</FONT></TD><TD WIDTH=180><FONT SIZE=5 FACE="Monaco">%</FONT>
</TD></TR>
<TR><TD WIDTH=187>Meaning</TD><TD WIDTH=60>+</TD><TD WIDTH=60>-
</TD><TD WIDTH=60><FONT SIZE=6 FACE="Symbol">¥</FONT></TD>
<TD WIDTH=60><FONT SIZE=6 FACE="Symbol">_</FONT></TD><TD WIDTH=180>remainder
</TD></TR>
</TABLE>
<P>
<P>
Examples:<BR>
<FONT SIZE=5 FACE="Monaco">6 + 1 </FONT><FONT SIZE=5 FACE="Symbol">Þ</FONT><FONT SIZE=5 FACE="Monaco">
77 - 2 </FONT><FONT SIZE=5 FACE="Symbol">Þ</FONT><FONT SIZE=5 FACE="Monaco">
57 * 2 </FONT><FONT SIZE=5 FACE="Symbol">Þ</FONT><FONT SIZE=5 FACE="Monaco">
1414 / 3 </FONT><FONT SIZE=5 FACE="Symbol">Þ</FONT><FONT SIZE=5 FACE="Monaco">
414 % 3 </FONT><FONT SIZE=5 FACE="Symbol">Þ</FONT><FONT SIZE=5 FACE="Monaco">
2<BR>
</FONT>
<P>
<FONT SIZE=6>Unary Operators<BR>
</FONT>
<TABLE BORDER=1>
<TR><TD WIDTH=187><FONT SIZE=6>Operator</FONT></TD><TD WIDTH=180><FONT SIZE=5 FACE="Monaco">+</FONT>
</TD><TD WIDTH=180><FONT SIZE=5 FACE="Monaco">-</FONT></TD></TR>
<TR><TD WIDTH=187>Meaning</TD><TD WIDTH=180>identity</TD><TD WIDTH=180>negation
</TD></TR>
</TABLE>
<P>
<P>
Examples:<BR>
<FONT SIZE=5 FACE="Monaco">-(2 + 3) </FONT><FONT SIZE=5 FACE="Symbol">Þ</FONT><FONT SIZE=5 FACE="Monaco">
-5+(2 * 3) </FONT><FONT SIZE=5 FACE="Symbol">Þ</FONT><FONT SIZE=5 FACE="Monaco">
6<BR>
<BR>
</FONT>
<P>
<FONT SIZE=6>Relational Operators<BR>
</FONT>
<TABLE BORDER=1>
<TR><TD WIDTH=187><FONT SIZE=6>Operator</FONT></TD><TD WIDTH=60><FONT SIZE=5 FACE="Monaco"><</FONT>
</TD><TD WIDTH=90><FONT SIZE=5 FACE="Monaco"><=</FONT></TD>
<TD WIDTH=60><FONT SIZE=5 FACE="Monaco">></FONT></TD><TD WIDTH=90><FONT SIZE=5 FACE="Monaco">>=</FONT>
</TD><TD WIDTH=90><FONT SIZE=5 FACE="Monaco">==</FONT></TD><TD WIDTH=90><FONT SIZE=5 FACE="Monaco">!=</FONT>
</TD></TR>
<TR><TD WIDTH=187>Meaning</TD><TD WIDTH=60><FONT SIZE=6 FACE="Symbol"><</FONT>
</TD><TD WIDTH=90><FONT SIZE=6 FACE="Symbol">£</FONT></TD>
<TD WIDTH=60><FONT SIZE=6 FACE="Symbol">></FONT></TD><TD WIDTH=90><FONT SIZE=6 FACE="Symbol">³</FONT>
</TD><TD WIDTH=90><FONT SIZE=6 FACE="Symbol">=</FONT></TD><TD WIDTH=90><FONT SIZE=6 FACE="Symbol">¹</FONT>
</TD></TR>
</TABLE>
<P>
<P>
ï In C, relational operations are just arithmetic operations:
they yield integer values.<BR>
<P>
ï C interprets 0 as logical "false". All other
integers are treated as logical "true". In particular,
relational operators use 1 to mean "true".<BR>
<P>
ï Remember that the equality relation is <FONT SIZE=6 FACE="Monaco">==</FONT><FONT SIZE=6>.
Using </FONT><FONT SIZE=6 FACE="Monaco">=</FONT><FONT SIZE=6>
means something else in C; never use </FONT><FONT SIZE=6 FACE="Monaco">=</FONT><FONT SIZE=6>
in an expression.<BR>
</FONT>
<P>
<FONT SIZE=6>Examples:<BR>
</FONT><FONT SIZE=5 FACE="Monaco">5 == 5 </FONT><FONT SIZE=5 FACE="Symbol">Þ</FONT><FONT SIZE=5 FACE="Monaco">
15 < 5 </FONT><FONT SIZE=5 FACE="Symbol">Þ</FONT><FONT SIZE=5 FACE="Monaco">
04 < 5 </FONT><FONT SIZE=5 FACE="Symbol">Þ</FONT><FONT SIZE=5 FACE="Monaco">
15 <= 5 </FONT><FONT SIZE=5 FACE="Symbol">Þ</FONT><FONT SIZE=5 FACE="Monaco">
1</FONT>
<H1><CENTER><FONT SIZE=6>Expressions in C ó Precedence
<BR>
</FONT></CENTER></H1>
<P>
<FONT SIZE=6>ï Parentheses can be used to group operators
explicitly.<BR>
</FONT>
<P>
<FONT SIZE=6>ï In the absence of parentheses, operator groupings
are determined by operator <I>precedence</I>.<BR>
</FONT>
<TABLE BORDER=1>
<TR><TD WIDTH=127><FONT SIZE=6>High</FONT></TD><TD WIDTH=120>...
</TD><TD WIDTH=120>...</TD><TD WIDTH=120>...</TD><TD WIDTH=120>Low
</TD></TR>
<TR><TD WIDTH=127><FONT SIZE=5 FACE="Monaco">+ - </FONT><FONT SIZE=5>(unary)</FONT>
</TD><TD WIDTH=120><FONT SIZE=5 FACE="Monaco">* / %</FONT></TD>
<TD WIDTH=120><FONT SIZE=5 FACE="Monaco">+ - </FONT><FONT SIZE=5>(binary)</FONT>
</TD><TD WIDTH=120><FONT SIZE=5 FACE="Monaco">< <= </FONT>
<P>
<FONT SIZE=5 FACE="Monaco">> >=</FONT>
</TD><TD WIDTH=120><FONT SIZE=5 FACE="Monaco">== !=</FONT></TD>
</TR>
</TABLE>
<P>
<P>
ï An operator with higher precedence groups "more tightly"
than an operator of lower precedence.<BR>
<P>
ï Operators of same precedence group to the left. <BR>
<P>
Example:<BR>
<FONT SIZE=5 FACE="Monaco">1 + 2 * 3 + 4 < - 5 - 6 - 7<BR>
</FONT>
<P>
<FONT SIZE=6>groups as<BR>
</FONT><FONT SIZE=5 FACE="Monaco">((1 + (2 * 3)) + 4) < (((-5)
- 6) - 7)</FONT>
<H1><FONT SIZE=6>The Two Uses of Programs <BR>
</FONT></H1>
<P>
<FONT SIZE=6>ï You can always add parentheses to make the
intended meaning clear.<BR>
</FONT>
<P>
<FONT SIZE=6>Example: 1+2*3 is the same as 1+(2*3) as far as
the computer is concerned.<BR>
</FONT>
<P>
<FONT SIZE=6>ï But, to realize that the value of this expression
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -