Table of Contents
- 1.Types of C++ Operators
- 2.Additional Operators in C++
- 3.C++ Operators Precedence and Associativity
C++ is very rich in operators. When a value is generated from an expression,operators are used to get appropriate value.Most languages use the special characters ‘+’ (plus) and ‘-‘ (minus) to represent the two basic arithmetic operations, but there is no uniformity in others. In general, there are four types of operators.
Types of C++ Operators
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
The Arithmetic operators in C++ deal with Addition (+), Subtraction (-), Multiplication (*), Division (/) and Modulus (%). All these operators are binary arithmetic operators, because they accept two operands (the things being operated). There is unary minus (-) and unary plus (+). They are applied to only one operand.
Usage of Arithmetic operators
a+b, a-b, a/b, a%b, -a, a
The division of integer by integer terminates fractional part. The expression such as a%b produces the remainder zero during exact division; otherwise the order of evaluation is performed as per precedence rule.
|+||Addition OR Unary Plus|
|–||Subtraction OR Unary Minus|
|*||Multiplication OR Indirection|
Modulus (Remainder of a division
They are used to compare two values, which may be variables, constants or expressions. All the Relational operators have lower precedence then the Arithmetic operators.
Usage of Relational Operators
The expressions such as I > m, I >= m, I < m, I <= m, I !=m are all examples of Relational operators.
|>||Greater Than||4 > 1||Test if 3 > 1||Yes|
|>=||Greater Than or Equal||4 >= 4||Test if 4 >=4||Yes|
|<||Less Than||4 < 6||Test if 4 < 6||Yes|
|<=||Less Than or Equal||4 <= 4||Test if 4 <=4||Yes|
|==||Equal||4 == 4||Test if 4 == 4||Yes|
|!=||Not Equal||4 != 5||Test if 4 != 5||Yes|
The above expressions can be any C++ expression, which can include a variable, litereal or a combination of these. It should also to be noted that the result of applying a comparison operator to two operands is a bool value that is either true or false (T/F).
The logical connectives ‘&&’ (and) and ‘||’ (or) are very useful operators. They are used with relational operators.
(i>5) && (i<10)
Means the expression is true if i>5 and i<10, otherwise false.
|&&||Perform Boolean (Logical) AND||(x > 5) && (x < 10)|
|||||Perform Boolean (Logical) OR||(x > 5) || (x < 10)|
|!||Perform Boolean (Logical) NOT||x != 10|
An expression such as x = x + 10, means add the contents of ‘x’ with ‘10’ and assign the result into ‘I’ where ‘=’ is the assignment operator. In layman words, first add whatever is stored in x with 10 and then the ‘=’ signs means to assign the new number to x. C++ offers a compressed for, which is a very convenient way by placing the operator immediately (no space between the two operators) before the assignment operator. These two operators are known as the compound assignment operator.
The general form of a compound assignment operator is;
Expression 1 operator = expression 2
Where operator is one of the following.
+ – * / % << >> & ^ |
If E1 is expression 1 and E2 is expression 2 then
e1 = (e1)op(e2)
a *= b + 1
a = a * (b+1)
rather than a = a * b + 1
Additional Operators in C++
The conditional operator is also known as ternary operator, because it makes three expressions such as
Expression1 ? expression2 : expression3
Actually the ternary operator “?”, provides an alternative way to if-else contructions. The expression1 is evaluated first. If it is true, then execute expression2, otherwise expression3.
Increment and Decrement Operators
As mentioned earlier that C++ language is very rich in operators. Here we discuss the two unusal operators for incrementing and decrementing variable. C++ uses not only the infix operator such as x = x + 1 but also the postfix and prefix operators. The operator ‘++’ adds 1 to its operand, operator ‘- -‘ subtracts 1 from its operand.
The use of “++”, and “- -“before the variable is called prefix and postfix operators. The inverse of both the operators is also true. The effect is the same but x = i++ and x = ++I are different in result.
If i = 10, then x = i++ menas set x to 10 and increment i by 1. This is called post-increment because the operator is placed after the variable.
If i = 10, then x = ++I, means set x to 11 that is first increment ‘i’ by 1 and then assign the result into ‘x’. this is called pre-increment because the operator is placed before the variable. The same is true for –.
In a context where no value is wanted, the effect of prefix, and postfix operators is the same. In many cases, preferable in for loop, we will see the extensive use of these two notations.
Bitwise Logical Operators
There are lot of operators for bit manipulations, such as
|^||Bitwise Exclusive OR|
|~||One’s complement (unary)|
It is a unary operator that gives the size of a variable or a data type. In either case, it must be within the parentheses.
This return the value 2, since type int occupies 2 bytes in memory.
This will return the value ‘1’ because type of ‘ch’ is char.
C++ Operators Precedence and Associativity
When priority of two operators is the same, the evaluation starts from left to right. For example, when addition and subtraction appears together in one expression, the expression is evaluated from left to right because of the same priority. Another good example is the use of multiplication and division. Operators can be applied to an expression to generate a value on one of these unary operator (one operand), binary (two operands) and ternary operator (three operands).
The precedence of each operator is strictly predefined that how the expression is evaluated containing different operators. The evaluation of expression is performed on two ways, left to right and right to left. This is known as operator Associativity.