C++ provides a rich operator environment. An operator is a symbol that tells the compiler to perform a specific mathematical or logical manipulation.
C++ defines the following arithmetic operators:
Operator | Description |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
++ | Increment |
-- | Decrement |
Relational operators evaluate values on the left and right side of the operator and return the relation as either True or False.
Operator | Description |
== | is equal to |
< | is less than |
> | is greater than |
<= | is less than or equal to |
>= | is greater than or equal to |
!= | is not equal to |
The logical operators are used to support the basic logical operations AND, OR, and NOT.
Operator | Description |
&& | AND |
|| | OR |
! | NOT |
These operators are useful when assigning values to variables.
var = expression; int x, y, z; x = y = z = 100; // set x, y, and z to 100
Compound Assignments
C++ provides special compound assignment operators that simplify the coding of certain assignment statements. Let’s begin with an example.
The assignment statement shown here:
x = x + 10; //can be written using a compound assignment as x += 10;