Number operations -- more widely known as arithmetic operations -- are about the same in C++ as they are anywhere else. You simply combine a pair of combinable values -- be they variables or constants -- via the various arithemtic operators (+, -, *, /, etc) and either store or display them.
In its simplest form, you can simply do the aforementioned operations within a cout statement; simply enclose the entire operation in << symbols and the program will print out the result. As I mentioned above, you can use either variables or constants or combinations of the two; the only requirements are that the variable is declared. Yes, you can perform math on an uninitialized variable, but there is no point in it as you'll have no idea what data will be in the variable if you don't initialize it.
To store the result of an arithmetic operation, you simply set it equal to a variable capable of holding it. It is fairly self explanatory except for two points. First, in programming, the equal sign (=) is known as the assignment operator, as that is its only purpose: to assign values. Second, the variable storing the result of an arithmetic operation must always be to the left of the assignment operator. Here is a short rundown of arithmetic operations in their simplest form:
int a,b,c; a = 1 + 2; b = a - 2; cout << "a = " << a << endl; cout << "b + 5 = " << b + 5 << endl;
Note that, unless otherwise specified, no assignment is performed in the absense of the assignment operator. That is, in the second cout statement, b is unchanged because it was never "assigned" anything via the assignment operator. Also note that only values to the left of the assignment operator are modified, such as in the statement b = a - 2; a is unchanged because it is on the right of the operator.
In addition to the simplest forms of math, most programming languages, C++ included, provide additional control. With such operators as modulus and incrementing, you can perform common/useful math easily, and with a condensed notation available for the four major arithmetic operations (+,-,*,/), you can shorten certain arithmetic operations.
First off, we'll look at modulus. Used with the percent sign (%), it essentially performs division on the surrounding numbers and returns the remainder rather than the quotient. For example, 7 % 2 equals 1; 7 / 2 is 3 with 1 left over. Likewise, 2 % 3 is 2; 2 / 3 is 0 with 2 left over.
We'll now look at incrementing and decrementing variables. The operators are ++ and --, respectively. They add and subtract one from the variable to which they are attached, respectively. Yes, they can only be used on variables, and they do modify the variable without the need for an assignment operator. In addition to that, they act differently based on whether they're a prefix or suffix to the variable. If it is a prefix to the variable, it is performed before all other math on th same line. Suffixed incrementors/decrementors are executed after the rest of the math on the same line. Here they are in action:
int a,b,c; a = 1; a++; b = 1 + ++a; c = 1 + b++;
First, you'll notice the statement a++. After it executes, a will equal 2. On the next line, a is again incremented to become 3 (remember, it is prefixed and is thusly executed prior to any other math on the line), which makes b equal 4. Finally, c equals 5; b remains 4 until the rest of the line has executed, then it is incremented to 5. Decrementers (--) are used identically; simply replace the ++ with -- and subtract one instead of adding one.
C++ also features a condensed notation for certain forms of arithmetic operations. Simply showing you will explain it better than I ever could, so here it is:
b += 5;
The above statement is an example of the condensed notation. It is the same as the following:
b = b + 5;
In case you weren't aware that such operations were legal, they are; you are permitted to use the same variable on both the left and right side of the assignment operator. It works exactly as you'd expect, too. The current value of the variable is used in all of the math, and then it is overwritten after the math is complete. For example, if b equalled 1 before executing the above line, it would equal 6 afterwards.
But I digress, the condensed notation does nothing unique; it simply shortens a common arithmetic operation. As mentioned above, it can also be used with any of the other 3 arithmetic operations:
a /= 3; a -= 2; a += 4; a *= 10;
Finally, we must look at order of operations in C++ (and programming in general). They are as follows: (), * and / and %, + and -, and finally, =. The program first executes math enclosed in parentheses (from left to right, of course), then executes multiplication, division, and modulus operations from left to right, then addition and subtraction (incrementers and decrementers are not included here), and lastly, assignment operators. Take the following equation:
a = 1 + 3 / (2 + 4) - 5 * 2; a = 1 + 3 / 6 - 5 * 2; a = 1 + .5 - 5 * 2; a = 1 + .5 - 10; a = 1.5 - 10; a = -8.5;
If a was a double, it would store -8.5. However, if it was an int, it would only store -8.