Game Development Reference
In-Depth Information
The assignment operator can also be used in some pieces of code that aren't necessarily very
readable. Take the following line, for instance:
number1 = number2 = number3 = 1;
C++ compilers will evaluate these statements from right to left. 1 is assigned to number3 , which
would be assigned to number2 , which would be assigned to number1 , and all three variables will end
up storing 1. I wouldn't recommend writing code like this.
Arithmetic Operators
C++ also allows us to carry out basic arithmetic operations and we have five of these available:
Addition operator
Subtraction operator
Multiplication operator
Division operator
Modulo operator
The Addition Operator
The addition operator behaves exactly as you would expect: It allows us to add two numbers
together. The addition operator can add a value from the right to a value on the left and the result
can be used in a statement, like this:
int sum = number1 + number2;
Here we have added number2 to number1 and stored the result in a third variable named sum .
It is also possible to use the same values in all of the places in our addition statement, for example:
int number = 1;
number = number + number;
Here we end up with 2 being stored in the variable number after this code has executed.
The Subtraction Operator
The subtraction operator works exactly like the addition operator, although unsurprisingly it subtracts
one number from another and creates a result:
int number1 = 2;
int number2 = 1;
int result = number1 - number2;
This code would cause the value 1 to be stored in the variable result .
 
Search WWH ::




Custom Search