Sei sulla pagina 1di 6

OPERATORS AND EXPRESSION

Operators - An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulation.

Expression – Expression is combination of operands (variables and constants) and operators.

Operators are special symbols used for specific purpose.

There are two types of operator :

 Unary operator – unary operators operates on single operands.

e.g. – the unary minus(-) operator, the logical not(!) operator.

 Binary operator – binary operators operates on double operands.

C++ is rich in operators, and provides six types of operators.

 Arithmetic Operators.
 Assignment Operators.
 Relational Operators.
 Bitwise Operators.
 Logical Operators.
 Misc Operators.

Arithmetic Operators – There are five Arithmetic Operators supported by the C++ language.

Addition(+), Subtraction (-), Multiplication (*), Division (/), Modulus (%).

 Operations of additions, subtraction, multiplications and division literally correspond


with their respective mathematical operators.
 Modulo operators, represented by a percentage sign (%), gives the remainder of a
division of two values

e.g. : a=11%3;

the variable ‘a’ will contain the value 2,since 2 is the remainder from dividing 11 between 3.

Assignment Operators – The Assignment Operator assigns a value to a variable.

e,g : a=5;

 the statement assigns the integer value 5 to the variable a,


 the most important rule when assigning is the right-to-left rule. The assignment
operations always takes place from right to left, andnever the other way.

The following Assignment Operators supported by C++ language :

Operators Discription Examples

Simple assignment operator,Assigns values C=A+B will assign value of A+B


=
from right side operands to left sid operands. into C

+= Add AND assignment operator,it adds right C+=A is equivalent to C=C+A


operands to the left side operand and assign
the result to left operand.

Substract AND assignment operator,it


-= substracts right operands from the left side C-=A is equivalent to C=C-A
operand and assign the result to left operand.

Multiply AND assignment operator,it C*=A is equivalent to C=C*A


*= multiplies right operands to the left side
operand and assign the result to left operand.

Divide AND assignment operator,It divides C/=A is equivalent to C=C/A


/= left operand with the right operand and
assign the result to the left operand.

Modulus AND assignment operator, It takes C%=A is equivalent to C=C%A


%= modulus using two operands and assign the
result to left operand

<<= Left shift AND assignment operator C<<=2 is same as C=C<<2

>>= Right shift AND assignment operator C>>=2 is same as C=C<<2

&= Bitwise AND assignment operator C&=2 is same as C=C&2

Bitwise exclusive OR and assignment


^= C^=2 is same as C=C^2.
operator.

|= Bitwise includive OR and assignment operator C|=2 is same as C=C|2

Relational Operators – In order to evaluate a comparison between two expressions, Relational


Operators can be used.The result of a relational opertion is a Boolen value that can only be true
or false,according to its Boolen result.

The following are the relational operators supported by C++ language

Assume variable A holds 5 and variable B holds 10 then;

Operators Discription Example

Checks if the values 0f two operands are equal


== (A==B) is not true
or not,if yes then condition becomes true

Checks if the values of two operands are equal


!= or not ,if values are not equal the condition (A!=B) is true.
becomes true.

Checks if the values of left operand is greater


> than the value of right operand,if yes then (A>B)is not true.
condition becomes true.

Checks if the value of left operand is less than


< the value of right operand .if yes then condition (A>B) is true
becomes true

>= Checks if the value of left operand is greater (A>=B) is not true
than or equal to the value of right operand, if
yes then condition becomes true.

Checks if the value of left operand is less than or


<= equal to the value of right operand, if yes than (A<=B) is true.
condition becomes true

Bitwise Operator – Bitwise Operators modify variables considering the bit patterns that
represent the values they store.

Bitwise operator works on bits and perform bit-by-bit operation.

C++ provides six Bitwise operators

Symbols Operators

& Bitwise AND

| Bitwise inclusive OR

^ Bitwise XOR (exclusive OR)

<< Left shift

>> Right shift

~ Bitwise NOT (one’s complement) (unary)

 Bitwise AND “&”

Bit a Bit b a & b (a AND b)

0 0 0

0 1 0

1 0 0

1 1 1

 Bitwise OR “|”

Bit a Bit b a | b (a OR b)

0 0 0

0 1 1

1 0 1

1 1 1

 Bitwise XOR “^”


Bit a Bit b a ^ b (a XOR b)

0 0 0

0 1 1

1 0 1

1 1 0

 Bitwise NOT “~”

0 1

1 0

 Bitwise Shift Operator

There are two bitwise shift operators

 Right shift (>>) – The symbol for right shift operator is>>. For its operation, it requires
two operands. It shifts in its left operand to the right. The number of places the bits are
shifted (i.e. the right operand). Thus by doing ch>>3 all the bits will be shifted towards
right by three places and so on.
 Left shift (<<) – The symbol of left shift operator is <<. It shifts each bit in its hand
operated to the left by the number of positions indicated by the right -hand operand. It
works opposite to that of right shift operator. Thus by doing

ch << 1 in the above example we have 11001010. Blank spaces generated are filled up by zeroes
as above.

Logical operators : These operators are used to perform logical operations on the given
expressions. There are 3 logical operators in C language. They are, logical AND (&&),
logical OR(||) and Logical NOT (!).

 NOT Operator (!) – This Operator is used to perform Boolean operation NOT, it has only
one operand, located at its right, and the only thing that it does is to inverse the value of
it, producing false if its operand is true and true if its operand is false. Basically, it
returns the opposite Boolean value of evaluating its operand.

Example (screenshot)

 The logical operators && and || are used when evaluating two expressions to obtain a
single relational result.

&& OPERATOR : This operation results true if both its two operands are true, and false
otherwise.

A B A && B

True True True


True False False

False True False

False False False

|| OPERATOR : This operation results true if either one of its two operands is true, thus being
false only when both operands are false themselves.

A B A || B

True True True

True False True

False True True

False False False

Misc Operator

 Sizeof - sizeof operator returns the size of a variable. e.g., sizeof(a), where ‘a’ is integer,
and will return 4.
 Condition? X : Y – condition operator (?). if Condition is true then it returns value of X
otherwise returns value of Y.
 Comma operator (,) – Comma Operator causes a sequences of operators to be
performed. The value of the entire comma expression is the value of the last expression
of the comma-separated list.
 dot (.) and -> (arrow) – Member operators are used to reference individual members of
classes, structures, and unions.
 Cast – casting operator converts one data typeto another data type. e.g., int(2.2000)
would return 2.
 Pointer operator (&) – pointer Operator ‘&’returns the address of a variable. e.g., &a;
will give actual address of the variable.
 Pointer operator (*) – pointer Operator ‘*’ is pointer to a variable. e,g.,*var; will pointer
to a variable var.

Operator Precedence in C++ : Operator precedence determines the grouping of terms in an


expression. This affects how an expression is evaluated. Certain Operators have higher
precedence than others;e.g., the multiplication operator has higher precedence than the
addition operator.

For example x = 7+3*2; here,x is assigned 3, not 20 because operator * has higher precedence
than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appearat the top of the table,those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

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

Bitwise AND & Left to Right

Bitwise XOR ^ Left to Right

Bitwise OR | 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

Potrebbero piacerti anche