operators

Operators in Programming

Relation Operators:

The following table shows all relation operators supported by any programming language.

OperatorDescription
== (equal to)Check if the two operands are equal.
!= (not equal to)Check if the two operands are not equal.
> (greater than)Check if the operand on the left is greater than the operand on the right.
< (less than)The check operand on the left is smaller than the right operand.
>= (greater than or equal to)Check left operand is greater than or equal to the right operand.
<= (less than or equal to)Check if the operand on left is smaller than or equal to the right operand.

 

For example, assume variable A holds 10 and variable B holds 20, then:

OperatorDescriptionExample
==Checks if the values of two operands are equal or not, if yes then the condition becomes true.(A == B) is not true.
!=Checks if the values of two operands are equal or not, if values are not equal then the condition becomes true.(A != B) is true.
>Checks if the value of the left operand is greater than the value of the right operand, if yes then the condition becomes true.(A > B) is not true.
<Checks if the value of the left operand is less than the value of the right operand, if yes then the condition becomes true.(A < B) is true.
>=Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then the condition becomes true.(A >= B)
is not true.
<=Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then the condition becomes true.( A =< B) is true.

 

Logical Operators:

The following table shows all logical operators supported by any programming language.

OperatorDescription
&& (logical and)Called Logical AND operator.
I (logical or)Called Logical OR Operator.
! (logical not)Called Logical NOT Operator.

 

For example, Assume Boolean variable A holds true and variable B holds false, then

OperatorDescriptionExample
&&If both operands are non-zero, then the condition becomes true.(A && B) is false
IIIf any of the two operands is non-zero, then the condition becomes true.(A I I B) is true
!Use to reverse the logical state of its operand. If a condition is true then the logical NOT operator will make false.!(A && B) is true

 

Bitwise Operators:

Any programming language defines several bitwise operators that can be applied to the integer types long, int, short, char and byte.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR
<<left shift
>>right shift

 

Now let’s see the truth table for bitwise &, I and ^

aba & ba | ba ^ b
00000
01011
10011
11110

 

The bitwise shift operators shift the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value that are to be shifted. Both operands have the same precedence. Example,

a = 0001000

b = 2

a << b = 0100000

a >> b = 0000010

 

Assignment Operators:

Which can be used to assign a value to a variable. The following table shows all assignment operators supported by any programming language.

OperatorDescriptionExample
=assigns values from right-side operands to left-side operandsa = b
+=adds the right operand to the left operand and assigns the result to lefta += b is same as a = a + b
-=subtracts the right operand from the left operand and assigns the result to the left operanda -= b is same as a = a – b
*=multiply the left operand with the right operand and assign the result to the left operanda *= b is same as a = a * b
/=divides the left operand with the right operand and assigns the result to the left operanda /= b is same as a = a / b
%=calculate modulus using two operands and assign the result to the left operanda %= b is same as a = a % b

 

Let’s suppose variable A holds 8 and B holds 3.

OperatorExampleResult
+=A += B o r A = A + B11
-=A -= 3 or A = A + 35
*=A *= 7 or A = A * 756
/=A /= B or A = A / B2
%=A %= 5 or A = A % 53
a=bValue of b will be assigned to a 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top