Worksheet W.A.4.1                                                    

 

ANSWER SHEET

 

precedence and assignment operators

 

 

Using precedence rules, determine the correct answer to the following expressions:

 

1.    8  +  3  *  6  /  5  %  6  -  9                       5.    12  *  3  %  8  / 2

 

      8  +  18  /  5  %  6  -  9                                     36  %  8  /  2

 

          8  +  3  %  6  -  9                                                4  /  2

 

               8  +  3  -  9                                                        2

 

                       2

 

 

2.    (8 * 3) / 9 + 2 * 5                                     6.    (12 * 3) % (8 / 2)

 

      24  /  9  +  2  *  5                                                  36  %  4

 

             2  +  10                                                               0

 

                 12

 

 

3.    (double)  9 / 4                                          7.    17.5  /  3.75  +  2

 

             2.25                                                                   6.67

 

 

4.    (int) 17.5 / 3                                             8.    12.5  %  3

 

            17  /  3                                                   illegal statement

 

                 5                                  the % operators requires integer operands

 

 

9.      Explain how this statement is evaluated:            a  =  b  =  2;

 

The assignment operator (=) is processed from right to left.  The statement could be rewritten as 

 

            a = (b = 2);

 

The expression, b = 2, returns the value of 2, which is then assigned to a.


Translate each of the following statements into C++ code.  Where appropriate, several versions will be requested:

 

10.    Increment number  by 10.  (2 versions)

 

         a.  longer version                                   b.  using assignment operator

 

         number = number + 10;                        number += 10;

 

 

11.    Increment count  by 1.  (2 versions)

 

         a.  longer version                                   b.  using increment operator

 

         count = count + 1;                                 ++count;     or     count++;

 

 

12.    Multiply base  by 2.  (2 versions)

 

         a.  longer version                                   b.  using assignment operator

        

         base = base * 2;                                    base *= 2;

 

 

 

Determine the result of the following fragments of code:

 

13.    final value of a & b                                14.    final value of a & b

 

         b = 2;                                                               b = 2;

         a = ++b;                                                           a = b++;

 

         a = 3, b = 3                                                      a = 2, b = 3

 

 

 

15.    What is the most important reason for using constants in a program?

 

 

A program which repeatedly uses a literal value such as a tax rate can be quickly modified by establishing that tax rate as a constant at the top of the program.  If literal values were used throughout the program, changing the tax rate would involve changing that literal value everywhere it appears in the program.  If that literal value were established as a constant, changing the constant once at the top of program would effect a change throughout the source code.