<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="id39762979">
  <name>Assignment Operators</name>
  <metadata>
  <md:version>1.1</md:version>
  <md:created>2008/05/12 09:38:10.677 GMT-5</md:created>
  <md:revised>2008/05/12 09:42:33.664 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="dtanh">
      <md:firstname>Anh</md:firstname>
      <md:othername>Tuan</md:othername>
      <md:surname>Duong</md:surname>
      <md:email>tam.vohoang@gmail.com</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="dtanh">
      <md:firstname>Anh</md:firstname>
      <md:othername>Tuan</md:othername>
      <md:surname>Duong</md:surname>
      <md:email>tam.vohoang@gmail.com</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  

  <md:abstract>In the last chapter, we explored how results are displayed and how numerical data are stored and processed using variables and assignment statements. In this chapter, we complete our introduction to the basics of C++ by presenting additional processing and input capabilities.</md:abstract>
</metadata>
  <content>
    <para id="id40495904"><emphasis>Assignment operator</emphasis> (=) is used for assignment a value to a variable and for performing computations.</para>
    <para id="id40495914">Assignment statement has the syntax:</para>
    <para id="id40495918">
      <code>variable = expression;</code>
    </para>
    <para id="id40495927"><emphasis>Expression</emphasis> is any combination of constants, variables, and function calls that can be evaluated to yield a result.</para>
    <para id="id40495938">Example:</para>
    <para id="id40495942">
      <code>length = 25;</code>
    </para>
    <para id="id40495951">
      <code>cMyCar = “Mercedes”;</code>
    </para>
    <para id="id40495961">
      <code>sum = 3 + 7;</code>
    </para>
    <para id="id40495970">
      <code>newtotal = 18.3*amount;</code>
    </para>
    <para id="id40495979">
      <code>slope = (y2 – y1)/(x2 – x1);</code>
    </para>
    <para id="id40495989">The order of events when the computer executes an assignment statement is </para>
    <para id="id40495994">- Evaluate the expression on the right hand side of the assignment operator. </para>
    <para id="id40496001">- Store the resultant value of the expression in the variable on the left hand side of the assignment operator. </para>
    <para id="id40496008">Note: </para>
    <para id="id40496013">1. It’s important to note that the equal sign in C++ does not have the same meaning as an equal sign in mathematics.</para>
    <para id="id40496024">2. Each time a new value is stored in a variable, the old one is overwritten.</para>
    <section id="id-570664440282">
      <name>Example</name>
      <para id="id40496043">
        <code>// This program calculates the volume of a cylinder,</code>
      </para>
      <para id="id40496051">
        <code>// given its radius and height</code>
      </para>
      <para id="id40496064">
        <code>#include &lt;iostream.h&gt;</code>
      </para>
      <para id="id40496072">
        <code>int main()</code>
      </para>
      <para id="id40496080">
        <code>{</code>
      </para>
      <para id="id40496088">
        <code>float radius, height, volume;</code>
      </para>
      <para id="id40496099">
        <code/>
      </para>
      <para id="id40496105">
        <code>radius = 2.5;</code>
      </para>
      <para id="id40496116">
        <code>height = 16.0;</code>
      </para>
      <para id="id40496127">
        <code>volume = 3.1416 * radius * radius * height;</code>
      </para>
      <para id="id40496138">
        <code>cout &lt;&lt; "The volume of the cylinder is " &lt;&lt; volume &lt;&lt; endl;</code>
      </para>
      <para id="id40496150">
        <code>return 0;</code>
      </para>
      <para id="id40496161">
        <code>}</code>
      </para>
      <para id="id40496169">The <emphasis>output</emphasis> of the above program:</para>
      <para id="id40496180">The volume of the cylinder is 314.16</para>
      <para id="id40496184">We can write <emphasis>multiple assignments</emphasis>, such as a = b = c = 25;. Because the assignment operator has a right-to-left associativity, the final evaluation proceeds in the sequence</para>
      <para id="id40496199">c = 25;</para>
      <para id="id40496204">b = 25;</para>
      <para id="id40496210">c = 25;</para>
    </section>
    <section id="id-17026387409">
      <name>Data Type Conversion across Assignment Operator</name>
      <para id="id40496227">Note that data type conversion can take place across assignment operators, that is, the value of the expression on the right side of the assignment operator is converted to the data type of the variable to the left side of the assignment operator.</para>
      <para id="id40496236">For example, if temp is an integer variable, the assignment temp = 25.89 causes the integer value 25 to be stored in the integer variable temp.</para>
    </section>
    <section id="id-299809890518">
      <name>Assignment Variations</name>
      <para id="id40496269">C++ also use a shorthand notation to perform an operation and an assignment at the same time. This is denoted by an operator followed by an equal sign. For example, to add 4 to the variable x and assign x to the result, you say: x += 4. Figure 1 illustrates assignment operator and all assignment variations.</para>
      <figure id="id40496305"><media type="image/png" src="graphics1.png">
          <param name="height" value="173"/>
          <param name="width" value="500"/>
        </media>
      <caption> Assignment variations</caption></figure>
      <para id="id40496329">Assignment statements such as sum += 10 or its equivalent, sum = sum + 10, are very common in C++ programming.</para>
    </section>
    <section id="id-449260209329">
      <name>Increment and decrement operators</name>
      <para id="id40496348">For the special case in which a variable is either increased or decreased by 1, C++ provides two unary operators: <emphasis>increment operator</emphasis> and <emphasis>decrement operator</emphasis>.</para>
      <figure id="id40496369"><media type="image/png" src="graphics2.png">
          <param name="height" value="93"/>
          <param name="width" value="375"/>
        </media>
      <caption> Increment and decrement operators</caption></figure>
      <para id="id40496393">The increment (++) and decrement (--) unary operators can be used as prefix or postfix operators to increase or decrease value.</para>
      <para id="id40496411">A <emphasis>prefix</emphasis> operator is placed <emphasis>before</emphasis> a variable and returns the value of the operand after the operation is performed.</para>
      <para id="id40496439">A <emphasis>postfix</emphasis> operator is placed <emphasis>after</emphasis> a variable and returns the value of the operand before the operation is performed.</para>
      <para id="id40496462">Prefix and postfix operators have different effects when used in a statement</para>
      <para id="id40496468">b = ++a;</para>
      <para id="id40496474">will first increase the value of a to 6, and then assign that new value to b. It is equivalent to</para>
      <para id="id40496480">a = a +1;</para>
      <para id="id40496486">b = a;</para>
      <para id="id40496492">On the other hand, execution of the statement</para>
      <para id="id40496497">b = a++;</para>
      <para id="id40496503">will first assign the value of 5 to b, and then increase the value of a to 6. It is now equivalent to </para>
      <para id="id40496509">b = a;</para>
      <para id="id40496515">a = a + 1;</para>
      <para id="id40496520">The decrement operators are used in a similar way.</para>
      <section id="id-540565277686">
        <name>Example</name>
        <para id="id40496536">
          <code>// Preincrementing and postincrementing</code>
        </para>
        <para id="id40496544">
          <code>#include &lt;iostream.h&gt;</code>
        </para>
        <para id="id40496552">
          <code>int main()</code>
        </para>
        <para id="id40496560">
          <code>{</code>
        </para>
        <para id="id40496568">
          <code>int c;</code>
        </para>
        <para id="id40496579">
          <code/>
        </para>
        <para id="id40496586">
          <code>c = 5;</code>
        </para>
        <para id="id40496597">
          <code>cout &lt;&lt; c &lt;&lt; endl; // print 5</code>
        </para>
        <para id="id40496613">
          <code>cout &lt;&lt; c++ &lt;&lt; endl; // print 5 then postincrement</code>
        </para>
        <para id="id40496628">
          <code>cout &lt;&lt; c &lt;&lt; endl &lt;&lt; endl; // print 6</code>
        </para>
        <para id="id40496640">
          <code/>
        </para>
        <para id="id40496646">
          <code>c = 5;</code>
        </para>
        <para id="id40496658">
          <code>cout &lt;&lt; c &lt;&lt; endl; // print 5</code>
        </para>
        <para id="id40496674">
          <code>cout &lt;&lt; ++c &lt;&lt; endl; // preincrement then print 6</code>
        </para>
        <para id="id40496689">
          <code>cout &lt;&lt; c &lt;&lt; endl; // print 6</code>
        </para>
        <para id="id40496705">
          <code/>
        </para>
        <para id="id40496712">
          <code>return 0; // successful termination</code>
        </para>
        <para id="id40496728">
          <code>}</code>
        </para>
        <para id="id40496735">The <emphasis>output</emphasis> of the above program:</para>
        <para id="id40496746">5</para>
        <para id="id40496750">5</para>
        <para id="id40496755">6</para>
        <para id="id40496759">5</para>
        <para id="id40496763">6</para>
        <para id="id40496767">6</para>
      </section>
    </section>
  </content>
</document>
