Game Development Reference
In-Depth Information
As you can see, the result is only false when all parameters in the || chain are false where they were
all true, to provide a true result using && .
Another class of operators we can use in C++ are the unary operators. So far all of the operators we
have looked at have required two input values to create an output value. Unary operators can work
on a single variable.
Unary Operators
C++ provides a set of operators that can be used with a single variable as opposed to the operators
that take two operands as we have seen so far. There are arithmetic, logical, and binary unary
operators, which we look at in this section. First we take a look at the arithmetic unary operators.
Arithmetic Unary Operators
The arithmetic unary operators are the plus and negative operators, and the increment and
decrement operators.
Unary Negative Operator
The unary negative operator is used to negate a given value just as we do in general arithmetic. -x
means negate x . If x happened to be 4 then the value would become -4, if it was -3 the value would
become 3. It can be used in C++ code as follows:
int negatedValue = -4;
int positiveValue = -negatedValue;
positiveValue would contain 4 after executing these lines of code.
Unary Plus Operator
The unary plus operator simply returns the same value as it reads and therefore finds little use in
practical programming.
int valueA = 1;
int valueB = +valueA;
In the preceding code, valueB would also contain the number 1.
The Increment and Decrement Operators
The increment operator allows us to increase a value by one and the decrement operator allows
us to decrease a value by one. If we place the operator before the variable name, it is said to be
a preincrement or predecrement operator. If we place it after it is said to be a postincrement or
postdecrement operator. Listing 3-13 shows a small MSVC program that outputs various values
using these operators.
 
Search WWH ::




Custom Search