Operator: Operators are used in C++ to carry out various functions. Mostly operators are used in arithmetic calculations and in logical expressions.
Unary operator: An operator that needs one operand. Examples: Unary -, unary +, ++, — and !. There are more unary operators, but they will be discussed later.
Binary operator: An operator that needs two operands. Example: Binary +, Binary -, *, /, %, C++ short hand operators, logical operators, && and ||. More binary operators will be discussed later.
Ternary operator: An operator that needs three operands. Ternary operator is also known as Conditional operator( ?)
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides following type of operators:
-
Arithmetic Operators
Operator | Description | Example |
---|---|---|
+ |
Adds two operands |
A + B will give 30 |
– |
Subtracts second operand from the first |
A – B will give -10 |
* |
Multiply both operands |
A * B will give 200 |
/ |
Divide numerator by de-numerator |
B / A will give 2 |
% |
Modulus Operator and remainder of after an integer division |
B % A will give 0 |
++ |
Increment operator, increases integer value by one |
A++ will give 11 |
— |
Decrement operator, decreases integer value by one |
A– will give 9 |
-
Relational Operators(<,<=,>,>=,==,!=)
Operator | Description | Example |
---|---|---|
== |
Checks if the value of two operands is equal or not, if yes then condition becomes true. |
(A == B) is not true. |
!= |
Checks if the value of two operands is equal or not, if values are not equal then condition becomes true. |
(A != B) is true. |
> |
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. |
(A > B) is not true. |
< |
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. |
(A < B) is true. |
>= |
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. |
(A >= B) is not true. |
<= |
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. |
(A <= B) is true. |
-
Logical Operators( AND, OR, NOT ,&& , ||, !)
Operator | Description | Example |
---|---|---|
&& |
Called Logical AND operator. If both the operands are non zero then condition becomes true. |
(A && B) is false. |
|| |
Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. |
(A || B) is true. |
! |
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. |
!(A && B) is true. |
-
Assignment Operator =
Operator | Description | Example |
---|---|---|
= |
Simple assignment operator, Assigns values from right side operands to left side operand |
C = A + B will assign value of A + B into C |
+= |
Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand |
C += A is equivalent to C = C + A |
-= |
Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand |
C -= A is equivalent to C = C – A |
*= |
Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand |
C *= A is equivalent to C = C * A |
/= |
Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand |
C /= A is equivalent to C = C / A |
%= |
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand |
C %= A is equivalent to C = C % A |
C++ Expression
Expressions are sequences of operators and operands that are used for one or more of these purposes:
- Computing a value from the operands.
- Designating objects or functions.
- Generating “side effects.” (Side effects are any actions other than the evaluation of the expression — for example, modifying the value of an object.)
Numeric Expression
A C++ expression involving Arithmetic Operators is called numeric expression. Any expression in C++ consists of operators and operands. Examples of C++ numeric expressions are given below:
Expression | Operator | Operands |
10 + 20 | + | 10 and 20 |
25 – 16 | – | 25 and 16 |
35 / 4.25 | / | 35 and 4.25 |
20 * 1.25 | * | 20.5 and 1.25 |
25 % 7 | % | 25 and 7 |
35 * 2 | * | 35 and 2 |
Pure Expression: An expression where all the operands belong to same data type.
Rule: int operator int = int
float operator float = float
double operator double = double
Examples of pure expressions:
Integer Type | Floating Point Type |
10 + 20 | 2.5 + 3.8 |
20 – 5 | 9.8 – 3.5 |
17 * 6 | 11.25 * 2.5 |
35 / 7 | 5.7 / 1.9 |
34 % 5 | 10.8 / 3.2 |
Mixed Expression: An expression where the operands belong to different data types.
Rule: int operator char = int
char operator int = int
int operator float = float
float operator int = float
int operator double = double
double operator int = double
Examples of mixed expressions:
32 + ‘A’ = 97 since ASCII code of ‘A’ is 65
‘t’ – 32 = 84 since ASCII code of ‘t’ is 116
20.0 / 8 = 2.5
20 + 2.5 = 22.5
C++ Shorthand: C++ allows an expression to be written in a compact form. C++ shorthand works with character (char) type data, integer (int) type data and floating point (float and double) type data. Examples of C++ shorthand are given below:
Operator |
Expression |
Expansion |
Meaning |
+= |
a += b |
a = a + b |
Variable a is assigned a value a + b |
-= |
a -= b |
a = a – b |
Variable a is assigned a value a – b |
*= |
a *= b |
a = a * b |
Variable a is assigned a value a * b |
/= |
a /= b |
a = a / b |
Variable a is assigned a value a / b |
%= |
a %= b |
a = a % b |
Variable a is assigned a value a % b |
As you will regularly combine operators on your various calculations, each operation is known for how much it “weighs” as compared to other operators. This is known as its precedence. This means that when a certain operator is combined with another, such as a + b * c, or x / y – z, some operators would execute before others, almost regardless of how you write the operation. That’s why an operator could be categorized by its level of precedence.
Precedence of operators.
Category | Operator | Associativity |
---|---|---|
Postfix |
() [] -> . ++ – – |
Left to right |
Unary |
+ – ! ~ ++ – – (type)* & sizeof |
Right to left |
Multiplicative |
* / % |
Left to right |
Additive |
+ – |
Left to right |
Shift |
<< >> |
Left to right |
Relational |
< <= > >= |
Left to right |
Equality |
== != |
Left to right |
Logical AND |
&& |
Left to right |
Logical OR |
|| |
Left to right |
Conditional |
?: |
Right to left |
Assignment |
= += -= *= /= %=>>= <<= &= ^= |= |
Right to left |
Comma |
, |
Left to right |
Increment Operator: Increment operator (++) increments value stored in a variable by 1 (One). Increment operator works with character (char) type data, integer (int) type data and floating point (float and double) type data. Examples of Increment operators are given below:
int a=10;
++a;
cout<<“Value in a=”<<a<<endl;
a++;
cout<<“Value in a=”<<a<<endl;
Produces output like
Value in a=11
Value in a=12
|
Let us assume that an integer variable x contains a value 6. The table given below displays the difference between pre-increment operator and post-increment operator.
Operator |
C++ Statement |
Output |
Explanation |
++ |
cout<<++x<<endl;cout<<x<<endl; |
77 |
Increments x and then displays xDisplays incremented values stored in x |
++ |
cout<<x++<<endl;cout<<x<<endl; |
67 |
Displays x and then increments xDisplays incremented values stored in x |
Decrement Operator: Decrement operator (–) decrements value stored in a variable by 1 (One). Decrement operator works with character (char) type data, integer (int) type data and floating point (float and double) type data. Examples of Decrement operators are given below:
int a=7;–a;cout<<“Value in a=”<<a<<endl;a–;cout<<“Value in a=”<<a<<endl; |
|
Produces output like
Value in a=6
Value in a=5
Let us assume that an integer variable z contains a value 26. The table given below displays the difference between pre-decrement operator and post-decrement operator.
Operator |
C++ Statement |
Output |
Explanation |
— |
cout<<–z<<endl;cout<<z<<endl; |
2525 |
Decrements z and then displays zDisplays decremented values stored in z |
— |
cout<<z–<<endl;cout<<z<<endl; |
2625 |
Displays z and then decrements zDisplays decremented values stored in z |
Previous |
Solved Examples |
Unsolved Questions for Practice |
Next |