11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

40 Part I: Introduction to C++ ProgrammingPerforming Simple Binary ArithmeticA binary operator is one that has two arguments. If you can say var1 op var2,op must be a binary operator. The most common binary operators are thesimple operations you performed in grade school. The binary operators areflagged in Table 3-1.Table 3-1 Mathematical Operators in Order of PrecedencePrecedence Operator Meaning1 + (unary) Effectively does nothing1 - (unary) Returns the negative of its argument2 ++ (unary) Increment2 -- (unary) Decrement3 * (binary) Multiplication3 / (binary) Division3 % (binary) Modulo4 + (binary) Addition4 - (binary) Subtraction5 =, *=,%=,+=,-= (special) Assignment typesMultiplication, division, modulus, addition, and subtraction are the operatorsused to perform arithmetic. In practice, they work just like the familiar arithmeticoperations as well. For example, using the binary operator for divisionwith a float variable looks like this:float var = 133 / 12;Each of the binary operators has the conventional meaning that you studiedin grammar school — with one exception. You may not have encounteredmodulus in your studies.The modulus operator (%) works much like division, except it produces theremainder after division instead of the quotient. For example, 4 goes into 15three times with a remainder of 3. Expressed in C++ terms, 15 modulus 4 is 3.int var = 15 % 4; // var is initialized to 3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!