There are four basic math operators that will be used in this tutorial. Those will the addition, subtraction, multiplication, and division operators.
Note:
variable_name OPERATOR= number;
If you replace operator with one of the proper math operators mentioned in this tutorial, it will do the proper operation to variable_name using number. Kinda hard to explain like that, here is an example:
int integer; // integer is now declared integer = 0; // integer is now 0 integer += 5; // integer is now 5 integer -= 2; // integer is now 3 integer *= 4; // integer is now 12 integer /= 2; // integer is now 6
Addition
Addition is the one of the most basic of the four mentioned above. Say you have two books.
__ ________ __ ________
| | ____ | | | ____ |
| | |____| | | | |____| |
| | | | | |
| | | | | |
| | | | | |
|_|________| |_|________|
Now you can get another book to put beside them.
__ ________ __ ________ __ ________
| | ____ | | | ____ | | | ____ |
| | |____| | | | |____| | | | |____| |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
|_|________| |_|________| |_|________|
Now you would have three books. You can do something similar in C++ like this:
2 + 1
To assign it to a variable, you simply declare it and them define it to the equation.
int my_int; my_int = 2 + 1;
Remember that the semicolon has to go after each command.
Did you know you can use + inside a wikicomplete page to make headings?
+ Heading 1
++ Heading 2
+++ Heading 3
++++ Heading 4
+++++ Heading 5
++++++ Heading 6
Note that you have to have that space after the +'s, or it won't work. Also, heading are put as part of the Table of Contents.
Subtraction
Subtraction is also a trivial operation. Remember those three books from above?
__ ________ __ ________ __ ________
| | ____ | | | ____ | | | ____ |
| | |____| | | | |____| | | | |____| |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
|_|________| |_|________| |_|________|
Let's say I decide not to check out two of them.
__ ________
| | ____ |
| | |____| |
| | |
| | |
| | |
|_|________|
This is just like getting that variable from earlier and taking two away.
my_int - 2;
Of course, if you wanted the variable to retain that value, you have three ways of doing it:
my_int -= 2; my_int = my_int - 2; my_int--; my_int--; my_int--; //Recommended to only do it this way if you need to subtract one. The same can be done for the addition operator.
Did you know you can use - inside wikicomplete pages to strike-through text?
--Text--
This would appear like this:
Text
Multiplication
Multiplication is basically mass addition. Maybe you have four sticks:
| | | |
If you multiply that by five, you would have twenty sticks. This basically just says 'add four sticks together five times'.
| | | | | | | | | | | | | | | | | | | |
This is, like all of these operators, simple to do in C++.
int my_other_int; my_other_int = 4; my_other_int *= 5;
Did you know you can use the multiplication operator inside wikicomplete pages to make text bold?
**Text**
It would appear like this:
Text
Division
Division if like the opposite of multiplication, a lot of subtracting. I have twenty sticks:
| | | | | | | | | | | | | | | | | | | |
I want to divide them among ten people. So I divide:
You got two sticks!
_
// | |
|/____
|____|
This can be done easily in C++:
my_other_int /= 10;
// my_other_int is now two
Did you know that you can use / inside wikicomplete pages to make text italic?
//Text//
This would appear like this:
Text