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

📄 tut1-3.html

📁 a Complete C++ language tutorial on the cplusplus.com
💻 HTML
📖 第 1 页 / 共 2 页
字号:
language, the relational operations did not return a <tt><b>bool</b></tt> value
<tt><b>true</b></tt> or <tt><b>false</b></tt>, rather they returned an
<tt><b>int</b></tt> as result with a value of <tt><b>0</b></tt> in order to represent
<b>"false"</b> and a value <u>different from <tt><b>0</b></tt></u> (generally
<tt><b>1</b></tt>) to represent <b>"true"</b>. For more information, or if your compiler
does not support the bool type, consult the section
<a href="../ansi/bool.html">bool type</a>.
</td></tr></table>
<p>

<dt><b>Logic operators ( <tt>!,  &amp;&amp;, ||</tt> ).</b><br>
<dd>
Operator <b>!</b> is equivalent to boolean operation NOT, it has only one
operand, located at its right, and the only thing that it does is to invert the value
of it, producing <tt><b>false</b></tt> if its operand is <tt><b>true</b></tt> and
<tt><b>true</b></tt> if its operand is <tt><b>false</b></tt>.
It is like saying that it returns the opposite result of evaluating its operand.
For example:<br>
<blockquote><table>
<tr><td><tt><b>!(5 == 5)</b></tt></td><td> returns <tt><b>false</b></tt> because the expression at its right <tt>(5 == 5)</tt> would be <tt><b>true</b></tt>.</td></tr>
<tr><td><tt><b>!(6 <= 4)</b></tt></td><td> returns <tt><b>true</b></tt> because <tt>(6 <= 4)</tt> would be <tt><b>false</b></tt>.</td></tr>
<tr><td><tt><b>!true</b></tt></td><td> returns <tt><b>false</b></tt>.</td></tr>
<tr><td><tt><b>!false</b></tt></td><td> returns <tt><b>true</b></tt>.</td></tr>
</table></blockquote>

Logic operators <tt><b>&amp;&amp;</b></tt> and <tt><b>||</b></tt>
are used when evaluating two expressions to obtain a single result. They
correspond with boolean logic operations <I>AND</I> and <I>OR</I> respectively.
The result of them depends on the relation between its two operands:<br>
<blockquote><table border="1">
<tr><td bgcolor="silver" align="center">First<br>Operand<br><b>a</b></td><td bgcolor="silver" align="center">Second<br>Operand<br><b>b</b></td><td bgcolor="silver" align="center"><tt>result<br><b>a &amp;&amp; b</b></tt></td><td bgcolor="silver" align="center"><tt>result<br><b>a || b</b></tt></td></tr>
<tr><td>true</td><td>true</td><td><b>true</b></td><td><b>true</b></td></tr>
<tr><td>true</td><td>false</td><td><b>false</b></td><td><b>true</b></td></tr>
<tr><td>false</td><td>true</td><td><b>false</b></td><td><b>true</b></td></tr>
<tr><td>false</td><td>false</td><td><b>false</b></td><td><b>false</b></td></tr>
</table></blockquote>
For example:<br>
<blockquote>
<tt><b>( (5 </B>==<B> 5) &amp;&amp; (3 &gt; 6) )</b></tt> returns <tt><b>false</b></tt> <tt>( <i>true</i> &amp;&amp; <i>false</i> )</tt>.<br>
<tt><b>( (5 </B>==<B> 5) || (3 &gt; 6))</b></tt> returns <tt><b>true</b></tt> <tt>( <i>true</i> || <i>false</i> )</tt>.<br>
</blockquote>

<p>
<dt><b>Conditional operator ( <tt>?</tt> ).</b><br>
<dd>
The conditional operator evaluates an expression and returns a different value according
to the evaluated expression, depending on whether it is <i>true</i> or <i>false</i>.
Its format is:
<blockquote><tt><i>condition <b>?</b> result1 <b>:</b> result2</i></tt></blockquote>
if <tt><i>condition</i></tt> is <tt><b>true</b></tt> the expression will return
<tt><i>result1</i></tt>, if not it will return <tt><i>result2</i></tt>.<br>
<blockquote><table>
<tr><td><tt><b>7</B>==<B>5 ? 4 : 3</b></tt></td><td> &nbsp; returns <b>3</b> since <b>7</b> is not equal to <b>5</b>.</td></tr>
<tr><td><tt><b>7</B>==<B>5+2 ? 4 : 3</b></tt></td><td> &nbsp; returns <b>4</b> since <b>7</b> is equal to <b>5+2</b>.</td></tr>
<tr><td><tt><b>5</B>&gt;<B>3 ? a : b</b></tt></td><td> &nbsp; returns <tt><b>a</b></tt>, since <b>5</b> is greater than <b>3</b>.</td></tr>
<tr><td><tt><b>a</B>&gt;<B>b ? a : b</b></tt></td><td> &nbsp; returns the greater one, <tt><b>a</b></tt> or <tt><b>b</b></tt>.</td></tr>
</table></blockquote>
<p>
<dt><b>Bitwise Operators ( <tt>&amp;, |, ^, ~, &lt;&lt;, &gt;&gt;</tt> ).</b><br>
<dd>Bitwise operators modify variables considering the bits that represent the values
that they store, that means, their binary representation.
<blockquote><table border="1">
<tr><td bgcolor="silver">op</td><td bgcolor="silver">asm</td><td bgcolor="silver">Description</td></tr>
<tr><td>&amp;</td><td><b>AND</b></td><td>Logical AND</td></tr>
<tr><td>|</td><td><b>OR</b></td><td>Logical OR</td></tr>
<tr><td>^</td><td><b>XOR</b></td><td>Logical exclusive OR</td></tr>
<tr><td>~</td><td><b>NOT</b></td><td>Complement to one (bit inversion)</td></tr>
<tr><td>&lt;&lt;</td><td><b>SHL</b></td><td>Shift Left</td></tr>
<tr><td>&gt;&gt;</td><td><b>SHR</b></td><td>Shift Right</td></tr>
</table></blockquote>
For more information about binary numbers and bitwise operations, consult
<a href="../papers/boolean.html">Boolean logic</a>.

<P>
<DT><B>Explicit type casting operators</B><BR>
<DD>
Type casting operators allows you to convert a datum of a given type to another.
There are several ways to do this in C++, the most popular one, compatible with the C language
is to precede the expression to be converted by the new type enclosed between
parenthesis <TT><B>()</B></TT>:
<BLOCKQUOTE><TT>
int i;<BR>
float f = 3.14;<BR>
i = (int) f;<BR>
</TT></BLOCKQUOTE>
The previous code converts the float number <TT><B>3.14</B></TT> to an integer value
(<TT><B>3</B></TT>). Here, the type casting operator was <TT><B>(int)</B></TT>.
Another way to do the same thing in C++ is using the constructor form: preceding the
expression to be converted by the type and enclosing <U>the expression</U> between
parenthesis:
<BLOCKQUOTE><TT>
i = int ( f );
</TT></BLOCKQUOTE>
Both ways of type casting are valid in C++. And additionally ANSI-C++ added new
type casting operators more specific for object oriented programming
(<A HREF="tut5-4.html">Section 5.4, Advanced class type-casting</A>).

<P>
<DT><B>sizeof()</B><BR>
<DD>
This operator accepts one parameter, that can be either a variable type or a variable
itself and returns the size in bytes of that type or object:
<BLOCKQUOTE><TT>a = sizeof (char);</TT></BLOCKQUOTE>
This will return <TT><B>1</B></TT> to <TT><B>a</B></TT> because <TT><B>char</B></TT> is
a one byte long type.<BR>
The value returned by <TT><B>sizeof</B></TT> is a constant, so it is always determined
before program execution.

<p>
<dt><b>Other operators</b><br>
<dd>
Later in the tutorial we will see a few more operators, like the ones referring to
pointers or the specifics for object-oriented programming. Each one is treated in its
respective section.

</dl>

<p>
<h2>Priority of operators</h2>
When making complex expressions with several operands, we may have some doubts
about which operand is evaluated first and which later. For example, in this expression:<br>
<blockquote><tt>a = 5 + 7 % 2</tt><br></blockquote>
we may doubt if it really means:<br>
<blockquote>
<tt>a = 5 + (7 % 2)</tt> with result <b>6</b>, or<br>
<tt>a = (5 + 7) % 2</tt> with result <b>0</b><br>
</blockquote>
The correct answer is the first of the two expressions, with a result of <tt><b>6</b></tt>.
There is an established order with the priority of each operator, and not
only the arithmetic ones (those whose preference we may already know from mathematics)
but for all the operators which can appear in C++. From greatest to lowest priority, the
priority order is as follows:
<blockquote><table border="1">
<tr><td bgcolor="silver"><b>Priority</b></td><td bgcolor="silver"><b>Operator</b></td><td bgcolor="silver"><b>Description</b></td><td bgcolor="silver"><b>Associativity</b></td></tr>
<tr><td>1</td><td><tt>::</tt></td><td>scope</td><td>Left</td></tr>
<tr><td>2</td><td><tt>() [ ] -&gt; . sizeof</tt></td><td>&nbsp;</td><td>Left</td></tr>
<tr><td rowspan="6">3</td>
 <td><tt>++ --</tt></td><td>increment/decrement</td><td rowspan="6">Right</td></tr>
 <tr><td><tt>~</tt></td><td>Complement to one (bitwise)</td></tr>
 <tr><td><tt>!</tt></td><td>unary NOT</td></tr>
 <tr><td><tt>&amp; *</tt></td><td>Reference and Dereference (pointers)</td></tr>
 <tr><td><tt>(<i>type</i>)</tt></td><td>Type casting</td></tr>
 <tr><td><tt>+ -</tt></td><td>Unary less sign</td></tr>
<tr><td>4</td><td><tt>* / %</tt></td><td>arithmetical operations</td><td>Left</td></tr>
<tr><td>5</td><td><tt>+ -</tt></td><td>arithmetical operations</td><td>Left</td></tr>
<tr><td>6</td><td><tt>&lt;&lt; &gt;&gt;</tt></td><td>bit shifting (bitwise)</td><td>Left</td></tr>
<tr><td>7</td><td><tt>&lt; &lt;= &gt; &gt;=</tt></td><td>Relational operators</td><td>Left</td></tr>
<tr><td>8</td><td><tt>== !=</tt></td><td>Relational operators</td><td>Left</td></tr>
<tr><td>9</td><td><tt>&amp; ^ |</tt></td><td>Bitwise operators</td><td>Left</td></tr>
<tr><td>10</td><td><tt>&amp;&amp; ||</tt></td><td>Logic operators</td><td>Left</td></tr>
<tr><td>11</td><td><tt>?:</tt></td><td>Conditional</td><td>Right</td></tr>
<tr><td>12</td><td><tt>= += -= *= /= %=<br>&gt;&gt;= &lt;&lt;= &amp;= ^= |=</tt></td><td>Assignation</td><td>Right</td></tr>
<tr><td>13</td><td><tt>,</tt></td><td>Comma, Separator</td><td>Left</td></tr>
</table></blockquote>
<P>
<i>Associativity</i> defines -in the case that there are several operators of the same
priority level- which one must be evaluated first, the rightmost one or the leftmost
one.<br>
<P>
All these precedence levels for operators can be manipulated or become more legible
using parenthesis signs <tt><B>(</B></tt> and <tt><B>)</B></tt>, as in this example:<br>
<blockquote>
<tt>a = 5 + 7 % 2;</tt><br>
</blockquote>
might be written as:<br>
<blockquote>
<tt>a = 5 + (7 % 2);</tt> or <br>
<tt>a = (5 + 7) % 2;</tt><br>
</blockquote>
according to the operation that we wanted to perform.
<P>
So if you want to write a complicated expression and you are not sure of the precedence
levels, always include parenthesis. It will probably also be more legible code.

<!--cuatut-->
<P>
<CENTER><TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 BORDER=0>
 <TR><TD BGCOLOR="#0000FF"><IMG SRC="head0.gif" WIDTH=2 HEIGHT=2></TD></TR>
 <TR><TD ALIGN="right"><FONT FACE="arial,helvetica" SIZE=1>&copy; The C++ Resources Network, 2000-2003 - All rights reserved</FONT></TD></TR>
</TABLE></CENTER>
<P>
<CENTER>
<TABLE CELLPADDING=0 WIDTH=100%>
<TR><TD ALIGN="right" WIDTH=45%><A HREF="tut1-2.html">
 <IMG SRC="butnback.gif" ALIGN="right" BORDER=0>
 Previous:<BR><B>1-2. Variables. Data types. Constants.</B></A></TD>
<TD ALIGN="center" WIDTH=10%><A HREF="index.html">
 <IMG SRC="butnindx.gif" BORDER=0><BR>
 index</A></TD>
<TD ALIGN="left" WIDTH=45%><A HREF="tut1-4.html">
 <IMG SRC="butnnext.gif" ALIGN="left" BORDER=0>
 Next:<BR><B>1-4. Communication through console.</B></A>
</TD></TR></TABLE>
</CENTER>
<!--/cuatut-->


</body>
</html>

⌨️ 快捷键说明

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