Adding, Subtracting, Dividing, and Multiplying
The four basic ways to manipulate numbers are adding, subtracting, dividing, and multiplying. By using these four mathematical operations, you can create any type of complicated mathematical formula.
To add, subtract, divide, or multiply two numbers (or two variables that represent numbers), you use the symbols shown in Table.
Remember The division symbol ( / ) usually appears in two places on your keyboard: on the same key as the question mark (?) and on the numeric keypad. The exponentiation symbol (^) appears on the 6 key. You can also use the subtraction symbol (–) to indicate negative numbers, such as –34.5 or –90.
Although you already understand how addition, subtraction, division, and multiplication work, you may be less familiar with integer division, modulo, and exponentiation.
Integer division divides two numbers and returns only the integer portion of the result. So dividing 25 by 6 would normally give you 4.167, but with integer division, the decimal portion of the number gets chopped off, and the result is simply 4.
Modulo is a form of division that divides two numbers and returns only an integer remainder. So dividing 25 mod 6 would return 1 (because 6 goes into 25 four times, leaving a remainder of 1).
Exponentiation simply multiplies one number by itself several times. The formula 4 ^ 3 tells the computer to take the number 4 and multiply it by itself three times. So 4 ^ 3 really means 4 * 4 * 4, or 64.
Mathematical operators, such as addition (+) and multiplication (*), are known as binary operators because they operate on two different values. In C++, you can also use something called unary operators, which operate on a single value. The most common C++ unary operators are ++ (which increments a value by one) and –, (which decrements a value by one).
You can place a unary operator before or after a variable (for example, ++i or i++). The placement of the unary operator defines when to alter the variable. Consider the following C++ code:
int x;
int y;
int z;
x = 5;
y = x++ // y = 5 but x = 6
z = ++y // z = 6 and y = 6
In the line y = x++, the value of x is 5 and is first assigned to y, which now holds the number 5. Then the ++ unary operator increments the value of x by one so x now stores the number 6. In the line z = ++y, the unary operator first increments the value of y. Because y holds the number 5, the ++ unary operator increments it to 6, and this value gets stored in the z variable.
Unary operators are often used to count in a FOR-NEXT loop although you can use them anywhere.
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply