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

📄 tut1-3.html

📁 a Complete C++ language tutorial on the cplusplus.com
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html>
<head>
<title>C++ tutorial: 1.3, Operators.</title>
<META NAME="description" CONTENT="special keywords and signs that conduct operations in C++: assignation, arithmetics, increase and decrese, relational, logic, conditional, bitwise and others. Priority.">
<META NAME="keywords" CONTENT="= + - * / % += -= *= /= %= &gt;&gt;= &lt;&lt;= &= ^= |= ++ -- == != &gt;= &lt;= &gt; &lt; ! && || ? & | ^ ~ &lt;&lt; &gt;&gt;">
</head>

<body bgcolor="white">

<!--captut-->
<CENTER>
<TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=1 BORDER=0>
<TR><TD WIDTH=90%>
 <FONT SIZE=4> Section 1.3 </FONT><BR>
 <FONT SIZE=5><B> Operators. </B></FONT>
</TD><TD><!--ad--><!--#include virtual="/ad/ad468.shtml"--><!--/ad-->
</TD><TD VALIGN="bottom"><A HREF="http://www.cplusplus.com/doc/tutorial/">
 <IMG SRC="head.gif" ALT="cplusplus.com" BORDER=0></A></TD></TR>
<TR><TD BGCOLOR="#0000FF" ALIGN="center" COLSPAN=3>
 <IMG SRC="head0.gif" WIDTH=2 HEIGHT=2 BORDER=0></TD></TR>
</TABLE>
</CENTER>
<!--/captut-->

<p>
Once we know of the existence of variables and constants we can begin to operate with them.
For that purpose, C++ provides the operators, which in this language are a set of
keywords and signs that are not part of the alphabet but are available in all
keyboards. It is important to know them since they are the basis of the C++ language.
<p>
You do not have to memorize all the content of this page, the details are
only provided to serve as a later reference in case you need it.
<dl>
<p>
<dt><b>Assignation (<tt>=</tt>).</b>
<dd>
The assignation operator serves to assign a value to a variable.<br>
<blockquote><tt>a = 5;</tt></blockquote>
assigns the integer value <B>5</B> to variable <b>a</b>. The part at the left of the
<tt>=</tt> operator is known as <I>lvalue</I> (left value) and the right one as
<i>rvalue</i> (right value). <i>lvalue</i> must always be a variable whereas the right side
can be either a constant, a variable, the result of an operation or any combination of
them.
<p>
It is necessary to emphasize that the assignation operation always takes place from
right to left and never at the inverse.<br>
<blockquote><tt>a = b;</tt></blockquote>
assigns to variable <tt><b>a</b></tt> (<i>lvalue</i>) the value that contains
variable <tt><b>b</b></tt> (<i>rvalue</i>) independently of the value that was stored
in <tt><b>a</b></tt> at that moment.  Consider also that we are only assigning the
<u>value</u> of <tt><b>b</b></tt> to <tt><b>a</b></tt> and that a later change of
<tt><b>b</b></tt> would not affect the new value of <tt><b>a</b></tt>.
<p>
For example, if we take this code (with the evolution of the variables' content in green
color):
<blockquote><tt><pre>
int a, b;    <font color="green"><i>// a:?  b:?</i></font>
a = 10;      <font color="green"><i>// a:10 b:?</i></font>
b = 4;       <font color="green"><i>// a:10 b:4</i></font>
a = b;       <font color="green"><i>// a:4 b:4</i></font>
b = 7;       <font color="green"><i>// a:4 b:7</i></font>
</pre></tt></blockquote>
will give us the result that the value contained in <tt><b>a</b></tt> is <tt><b>4</b></tt>
and the one contained in <tt><b>b</b></tt> is <tt><b>7</b></tt>. The final modification
of <tt><b>b</b></tt> has not affected <tt><b>a</b></tt>, although before we have declared
<tt><b>a = b;</b></tt> (right-to-left rule).
<p>
A property that C++ has over other programming
languages is that the assignation operation can be used as the <i>rvalue</i> (or part of
an <i>rvalue</i>) for another assignation.  For example:
<blockquote><tt>a = 2 + (b = 5);</tt></blockquote>
is equivalent to:
<blockquote><tt>b = 5;<br>a = 2 + b;</tt></blockquote>
that means: first assign <tt><b>5</b></tt> to variable <tt><b>b</b></tt> and then
assign to <tt><b>a</b></tt> the value <tt><b>2</b></tt> plus the result of the
previous assignation of <tt><b>b</b></tt> (that is 5), leaving
<tt><b>a</b></tt> with a final value of <tt><b>7</b></tt>. Thus, the following expression
is also valid in C++:
<blockquote><tt>a = b = c = 5;</tt></blockquote>
assigns 5 to the three variables <TT><b>a</b></TT>, <TT><b>b</b></TT> and <TT><b>c</b></TT>.
<p>
<dt><b>Arithmetic operators (<tt> +, -, *, /, % </tt>)</b><br>
<dd>
The five arithmetical operations supported by the language are:
<blockquote><table>
<tr><td>+</td><td>addition</td></tr>
<tr><td>-</td><td>subtraction</td></tr>
<tr><td>*</td><td>multiplication</td></tr>
<tr><td>/</td><td>division</td></tr>
<tr><td>%</td><td>module</td></tr>
</table></blockquote>
Operations of addition, subtraction, multiplication and division should not suppose an
understanding challenge for you since they literally correspond with their respective
mathematical operators.
<p>
The only one that may not be known by you is the <b>module</b>,
specified with the percentage sign (%). Module is the operation that gives the remainder of
a division of two integer values.  For example, if we write
<tt><b>a = 11 % 3;</b></tt>, the variable <tt><b>a</b></tt>
will contain <tt><b>2</b></tt> as the result since <tt><b>2</b></tt> is the remainder from
dividing 11 between 3.

<p>
<dt><b>Compound assignation operators (<tt>+=, -=, *=, /=, %=, &gt;&gt;=, &lt;&lt;=, &amp;=, ^=, |=</tt>)</b><br>
<dd>
A feature of assignation in C++ that contributes to its fame of sparing language
when writing are the compound assignation operators
(<b><tt>+=</tt></b>, <b><tt>-=</tt></b>, <b><tt>*=</tt></b> and <b><tt>/=</tt></b> among others),
which allow to modify the value of a variable with one of the basic operators:<br>
<blockquote>
<tt><b>value += increase;</b></tt> is equivalent to <tt><b>value = value + increase;</b></tt><br>
<tt><b>a -= 5;</b></tt> is equivalent to <tt><b>a = a - 5;</b></tt><br>
<tt><b>a /= b;</b></tt> is equivalent to <tt><b>a = a / b;</b></tt><br>
<tt><b>price *= units + 1;</b></tt> is equivalent to <tt><b>price = price * (units + 1);</b></tt><br>
</blockquote>
and the same for all other operations.
<p>
<dt><b>Increase and decrease.</b><br>
<dd>
Another example of saving language when writing code are the <I>increase</I> operator
(<TT><B>++</B></TT>) and the <i>decrease</i> operator (<TT><B>--</B></TT>). They
increase or reduce by 1 the value stored in a variable. They are equivalent
to <tt><b>+=1</b></tt> and to <tt><b>-=1</b></tt>, respectively.  Thus:<br>
<blockquote><tt>
a++;<br>a+=1;<br>a=a+1;
</tt></blockquote>
are all equivalent in its functionality: the three increase by 1 the value of <tt><b>a</b></tt>.
<p>
Its existence is because in the first C compilers the three previous
expressions produced different executable code according to which one was used.
Nowadays this type of code optimization is generally done automatically by the compiler.
<p>
A characteristic of this operator is that it can be used both as a <i>prefix</i> or
as a <i>suffix</i>. That means it can be written before the variable identifier
(<tt><b>++a</b></tt>) or after (<tt><b>a++</b></tt>). Although in simple expressions
like <tt><b>a++</b></tt> or <tt><b>++a</b></tt> they have exactly the same meaning, in
other operations in which the result of the <i>increase</i> or <i>decrease</i> operation
is evaluated as another expression they may have an important difference in their meaning:
In case that the increase operator is used as a <i>prefix</i> (<tt><b>++a</b></tt>)
the value is increased before the expression is evaluated and therefore the
increased value is considered in the expression; in case that it is used as a
<i>suffix</i> (<tt><b>a++</b></tt>) the value stored in <TT><B>a</B></TT> is increased after
being evaluated and therefore the value stored before the increase
operation is evaluated in the expression. Notice the difference:
<blockquote>
<table width="50%" cellspacing="5" border="0"><tr><td width="50%" align="center"><b><u>Example 1</u></b></td>
 <td width="50%" align="center"><b><u>Example 2</u></b></td></tr>
<tr><td bgcolor="#FFFFBF" width="50%"><tt><b>B=3;<br>A=++B;</b><br><I>// A is 4, B is 4</I></tt></td>
 <td bgcolor="#FFFFBF" width="50%"><tt><b>B=3;<br>A=B++;</b><br><I>// A is 3, B is 4</I></tt></td></tr></table>
</blockquote>
In Example 1, <tt><b>B</b></tt> is increased before its value is copied to <tt><b>A</b></tt>.
While in Example 2, the value of <tt><b>B</b></tt> is copied to <tt><b>A</b></tt> and
<TT><B>B</B></TT> is later increased.
<p>
<dt><b>Relational operators ( <tt>==, !=,  &gt;, &lt;, &gt;=, &lt;=</tt> )</b><br>
<dd>
In order to evaluate a comparison between two expressions we can use the Relational
operators. As specified by the ANSI-C++ standard, the result of a relational operation is a
<tt><b>bool</b></tt> value that can only be <tt><b>true</b></tt> or <tt><b>false</b></tt>,
according to the result of the comparison.
<p>
We may want to compare two expressions, for example, to know if they are equal or if one
is greater than the other. Here is a list of the relational operators that can be performed
in C++:
<blockquote><table>
<tr><td><tt>==</tt></td><td>Equal</td></tr>
<tr><td><tt>!=</tt></td><td>Different</td></tr>
<tr><td><tt>&gt;</tt></td><td>Greater than</td></tr>
<tr><td><tt>&lt;</tt></td><td>Less than </td></tr>
<tr><td><tt>&gt;=</tt></td><td>Greater or equal than</td></tr>
<tr><td><tt>&lt;=</tt></td><td>Less or equal than</td></tr>
</table></blockquote>
Here you have some examples:<br>
<blockquote><table>
<tr><td><tt><b>(7 </B>==<B> 5)</b></tt></td><td> would return <tt><b>false</b></tt>.</td></tr>
<tr><td><tt><b>(5 &gt; 4)</b></tt></td><td> would return <tt><b>true</b></tt>.</td></tr>
<tr><td><tt><b>(3 != 2)</b></tt></td><td> would return <tt><b>true</b></tt>.</td></tr>
<tr><td><tt><b>(6 &gt;= 6)</b></tt></td><td> would return <tt><b>true</b></tt>.</td></tr>
<tr><td><tt><b>(5 &lt; 5)</b></tt></td><td> would return <tt><b>false</b></tt>.</td></tr>
</table></blockquote>
of course, instead of using only numberic constants, we can use any valid expression,
including variables. Suppose that
<tt><b>a=2</b></tt>, <tt><b>b=3</b></tt> and <tt><b>c=6</b></tt>,<br>
<blockquote><table>
<tr><td><tt><b>(a </B>==<B> 5)</b></tt></td><td> would return <tt><b>false</b></tt>.</td></tr>
<tr><td><tt><b>(a*b &gt;= c)</b></tt></td><td> would return <tt><b>true</b></tt> since <tt>(2*3 &gt;= 6)</tt> is it.</td></tr>
<tr><td><tt><b>(b+4 &gt; a*c)</b></tt></td><td> would return <tt><b>false</b></tt> since <tt>(3+4 &gt; 2*6)</tt> is it.</td></tr>
<tr><td><tt><b>((b=2) </B>==<B> a)</b></tt></td><td> would return <tt><b>true</b></tt>.</td></tr>
</table></blockquote>
Be aware. Operator <tt>=</tt> (one equal sign) is not the same as operator <tt>==</tt>
(two equal signs), the first is an assignation operator (assigns the right side of the
expression to the variable in the left) and the other (<tt>==</tt>) is a relational operator
of equality that compares whether both expressions in the two sides of the operator are equal
to each other. Thus, in the last expression <tt>((b=2) == a)</tt>, we first assigned
the value <TT><B>2</B></TT> to <tt><b>b</b></tt> and then we compared it to
<tt><b>a</b></tt>, that also stores value <tt><b>2</b></tt>, so the result of the operation
is <tt><b>true</b></tt>.
<p>
<table width=95%><tr><td bgcolor="#BFFFBF">
<img src="icoansi.gif" align="left">
In many compilers previous to the publication of the ANSI-C++ standard, as well as in the C

⌨️ 快捷键说明

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