Orders of Operation

Just as in your high school (or college) algebra class, computers follow an order of operation for math as well. Here is the breakdown of precedence (or order of evaluation) for operators in C++ (we have not covered most of these, but do not fret, we will):

Operators                               Type                   Associativity
-----------------------------------------------------------------------------
( )                                     parentheses            left to right
++   --   +   -   static_cast<type>( )  unary                  right to left
*    /    %                             multiplicative         left to right
+    -                                  additive               left to right
<<   >>                                 insertion/extraction   left to right
<    <=   >   >=                        relational             left to right
==   !=                                 equality               left to right
?:                                      conditional            right to left
=    +=   -=  *=  /=  %=                assignment             right to left
,                                       comma                  left to right
-----------------------------------------------------------------------------


(  )      The expressions contained in parentheses are evaluated first in any statement.  
          The contents (innards) of the parentheses are subject to the same order of 
          operations, but they are evaluated before anything is evaluated 
          outside of the parentheses, and parentheses within parentheses
          are evaluated first, and so on and so forth.


++   --   +   -    Unary.  You will see more of these.  You are already 
                   familiar with static_cast( ) from the last 
                   problem, so now you know when this would get 
                   evaluated in the order of operations.  The ++  and -- 
                   will become familiar to you very soon, when we get 
                   into control structures.  And notice that the + and -
                   denoted here are for positive and negative numbers.


*   /   %     Multiplicative.  You should already know * and /.  The % 
              means modulus, and you will see that in your C++ travels.
              The short and sweet of it is that it gives a remainder.  For
              example, 5 % 2 mathmatically would be 2 remainder 1 (2 
              goes into 5 two times, leaving 1 left over).  The value
              returned for 5 % 2 in C++ would be 1.


+   -     Additive.  You had better know these, or go back to the books. :)


<<   >>     Insertion/extraction.  You have already seen these used in
            the iostream.  cin >> is insertion and cout << is extraction.


<   <=   >   >=     Relational.  Less than, less than or equal to, greater 
                    than, greater than or equal to.


==   !=     Equality.  Is equal to and is not equal to.  Used in evaluating
            statements as true or false.


?:     Conditional.  You will see this much later on as a shortcut, or 
       shorthand, for a if()..else statement.


=   +=   -=   *=   /=   %=    Assignment.  We'll show examples of these
                              later.  For now, you already know =.  The 
                              others we will get into in a little while.


,     Comma.  The last on the list and the last operation to be evaluated.
      You will see the comma used in control structures.


Consider the following operation for the equation of a straight line:

   y = mx + b

In C++, this would read as:

   y = m * x + b;

No parentheses are required because multiplication is applied before addition; it has a higher precedence. If you were to place parentheses around it, you would do so like this:

   y = ( m * x ) + b;

Which would be evaluated to the same thing. But... if you mistakenly did this:

   y = m * ( x + b );

This is entirely different. If m = 3, x = 2, and b = 5, the first two expressions would give 3 * 2 = 6 + 5 = 11, whereas the third expression would give 2 + 5 = 7 * 3 = 21. Big difference when you think about an expression like this one:

   x = p / q % s - t * z + y / 3 * g

What goes first? Well, the first operator we see is the = operator. That one is way down the list and lower than all other operators in this equation, so we'll do that last. Next is /, but right after that one is %, which are at the same level. Therefore they are evaluated left to right, first do p / q then the answer % s. - is next, but there are other operators that must go before it, so then we take t * z. You get the picture.

One thing a lot of programmers do is place the parentheses around areas of expressions to help clarify the order of mathematical operations. You cannot do this in the presence of unary operators ( ++ -- + - and the static_cast<type>( ) operators), though. For example, take the last expression:

   x = ( p / q % s ) - ( t * z ) + ( y / 3 * g )

It makes it much easier to see what should go first. Be very careful, though, not to change the natural order of operations in doing so!

Now a quick lesson in using constants.