OPERATORS
Operators are special symbols used for specific purposes. C++ provides six types of operators.
Arithmetical operators, Relational operators, Logical operators, Unary operators, Assignment operators, Conditional operators, Comma operator
Arithmetical operators, Relational operators, Logical operators, Unary operators, Assignment operators, Conditional operators, Comma operator
Arithmetical operators
An operator that performs an arithmetic (numeric) operation: +, -,*,/, or %. For these-operations always two or more than two operands are required. Therefore these operators are called binary operator.
Relational operators
The relational operators are used to test the relation between two values. All relational operators are binary operators and therefore require two operands. A relational expression returns zero when the relation is false and a non-zero when it is true. The following table shows the relational operators.
Relational Operators | Meaning |
< | Less than |
<= | Less than or equal to |
== | Equal to |
> | Greater than |
>= | Greater than or equal to |
! = | Not equal |
Logical operators
The logical operators are used to combine one or more relational expression. The logical operators are
Operators | Meaning |
|| | OR |
&& | AND |
! | NOT |
Unary operators
C++ provides two unary operators for which only one variable is required.
For Example
a = - 50;
a = + 50;
Here plus sign (+) and minus sign (-) are unary because they are not used between two variables.
For Example
a = - 50;
a = + 50;
Here plus sign (+) and minus sign (-) are unary because they are not used between two variables.
Assignment operator
The assignment operator '=' stores the value of the expression on the right hand side of the equal sign to the operand on the left hand side.
Example
int m = 5, n = 7; int x, y, z; x = y = z = 0;
in addition to standard assignment operator shown above, C++ also support compound assignment operators.
Example
int m = 5, n = 7; int x, y, z; x = y = z = 0;
in addition to standard assignment operator shown above, C++ also support compound assignment operators.
Compound Assignment Operators
C++ provides two special operators viz '++' and '--' for incrementing and decrementing the value of a variable by 1. The increment/decrement operator can be used with any type of variable but it cannot be used with any constant.
With the prefix version of these operators, C++ performs the increment or decrement operation before using the value of the operand For instance the following code
int sum, ctr;
sum = 12; ctr = 4;
sum= sum + ++ctr;
will produce the value of sum as 15 because ctr will be first incremented and then added to sum producing value 15.
int sum, ctr;
sum = 12; ctr = 4;
sum= sum + ++ctr;
will produce the value of sum as 15 because ctr will be first incremented and then added to sum producing value 15.
Similarly; the following code
sum = 12; ctr = 4;
sum = sum + - - ctr
will produce the value of sum as 15 because ctr will be first decremented.
sum = 12; ctr = 4;
sum = sum + - - ctr
will produce the value of sum as 15 because ctr will be first decremented.
With the postfix version of these operators, C++ first uses the value of the operand in evaluating the expression before incrementing or decrementing the operand's value. For example, the following code
sum = 12; ctr = 4;
sum = sum + ctr + +
will produce the value of sum as 16 because ctr will be first used in the expression producing the value of sum as 16 and then increment the value of ctr by 1 (ctr becomes now 5)
sum = 12; ctr = 4;
sum = sum + ctr + +
will produce the value of sum as 16 because ctr will be first used in the expression producing the value of sum as 16 and then increment the value of ctr by 1 (ctr becomes now 5)
Similary, the following code
sum = 12; ctr = 4;
sum = sum + ctr - -;
will produce the value of sum as 16 because ctr will be first used with its value 4 producing value of sum as 16 and then decrement the value of ctr by 1 (ctr becomes 3).
sum = 12; ctr = 4;
sum = sum + ctr - -;
will produce the value of sum as 16 because ctr will be first used with its value 4 producing value of sum as 16 and then decrement the value of ctr by 1 (ctr becomes 3).
C++ Shorthand
In the following table
Operator | Example | Equivalent to |
+ = | A + = 2 | A = A + 2 |
- = | A - = 2 | A = A - 2 |
% = | A % = 2 | A = A % 2 |
/= | A/ = 2 | A = A / 2 |
*= | A * = 2 | A = A * 2 |
Conditional operator
The conditional operator ?: is called ternary operator as it requires three operands. The format of the conditional operator is:
Conditional_ expression ? expression1 : expression2;
If the value of conditional expression is true then the expression1 is evaluated, otherwise expression2 is evaluated.
Example
int a = 5; int b = 6; big = (a > b) ? a : b;
The condition evaluates to false, therefore biggets the value from b and it becomes 6.
Conditional_ expression ? expression1 : expression2;
If the value of conditional expression is true then the expression1 is evaluated, otherwise expression2 is evaluated.
Example
int a = 5; int b = 6; big = (a > b) ? a : b;
The condition evaluates to false, therefore biggets the value from b and it becomes 6.
The comma operator
The comma operator gives left to right evaluation of expressions.
It enables to put more than one expression separated by comma on a single line.
Example
int i = 20, j = 25;
int sq = i * i, cube = j * j * j;
In the above statements, comma is used as a separator between two statements / expressions.
It enables to put more than one expression separated by comma on a single line.
Example
int i = 20, j = 25;
int sq = i * i, cube = j * j * j;
In the above statements, comma is used as a separator between two statements / expressions.
The sizeof operator
As we know that different types of Variables, constant, etc. require different amounts of memory to store them The sizeof operator can be used to find how many bytes are required for an object to store in memory.
Example
sizeof (char) returns 1
sizeof (int) returns 2
sizeof (float) returns 4
If k is integer variable, the sizeof (k) returns 2.
the sizeof operator determines the amount of memory required for an object at compile time rather than at run time.
Example
sizeof (char) returns 1
sizeof (int) returns 2
sizeof (float) returns 4
If k is integer variable, the sizeof (k) returns 2.
the sizeof operator determines the amount of memory required for an object at compile time rather than at run time.
The order of Precedence
The order in which the Arithmetic operators (+,-,*,/,%) are used in a. given expression is called the order of precedence. The following table shows the order of precedence.
Order | Operators |
First Second Third | () *, /, % +, - |
The following table shows the precedence of operators.
++, --(post increment/decrement) |
Highest
To
Lowest
|
++ (Pre increment) -- (Pre decrement), sizeof ( ), !(not), -(unary), +(unary) | |
*,/, % | |
+, - | |
<, <=, >, >= | |
==,!= | |
&& | |
? : | |
= | |
Comma operator |
No comments:
Post a Comment