Operators in c++
C++ provides a rich operator environment. An operator is a symbol that tells the compiler to perform a specific mathematical or logical manipulation.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Compound Assignments
- Increment / Decrement Operators
- C++ defines the following arithmetic operators:
- Relational operators evaluate values on the left and right side of the operator and return the relation as either True or False.
||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.
- 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
x = x + 10;
//can be written using a compound assignment as
x += 10;
Increment / Decrement Operators
- The increment operator ++ modifies the operand by adding 1 to its value and cannot be used with constants for this reason.
- Given that i is a variable, both i++ (postfix notation) and ++i (prefix notation) raise the value of i by 1. In both cases the operation i=i+1 is performed.
++i i is incremented first and the new value of i is then applied,
i++ the original value of i is applied before i is incremented.