Operators are nothing but symbols that perform specific operations. In this chapter let us discuss in detail the different types of operators Basic terminologies that we are going to use often are operator and operand If X+Y is the operation X and Y are the operands,+ is the operator
The following section will describe the types of operators
1. Arithmetic Operators
Arithmetic operators perform mathematical functions like addition, subtraction, multiplication, division, and modulor division. It requires a minimum of two operands.
Operator |
Name |
Description |
+ |
Additive operator |
performs addition and is also used for String concatenation |
- |
Subtraction operator |
performs subtraction of operands |
* |
Multiplication operator |
performs multiplication of operands |
/ |
Division operator |
performs division of operands |
% |
Remainder operator |
Performs the division of two operands x and y and returns the remainder value |
Program:
OUTPUT:
2. Unary Operators
The unary operators perform various operations such as incrementing or decrementing a value by one, negating an expression, or inverting the value of a boolean. A unary operator requires only one operand.
Operator |
Name |
Description |
+ |
Unary plus operator |
indicates positive value |
- |
Unary minus operator |
indicates negative value |
++ |
Increment operator |
increments a value by 1 |
-- |
Decrement operator |
decrements a value by 1 |
! |
Logical complement operator |
inverts the value of a boolean |
Program:
OUTPUT:
3. The Equality and Relational Operators:
The equality and relational operators are used to determine whether one operand is greater than or less than equal to or not equal to another operand. Here equal to is represented as == not =
Operator |
Name |
Description |
== |
equal to |
checks whether the value of two operands is equal |
!= |
not equal to |
checks whether the value of two operands is equal |
> |
greater than |
checks whether the value of one operand is greater than the second operand |
>= |
greater than or equal to |
checks whether the value of one operand is greater than or equal to the second operand |
< |
less than |
checks whether the value of one operand is less than the second operand |
<= |
less than or equal to |
checks whether the value of one operand is less or equal to the second operand |
Program:
In the above sample, only the true statements will be executed.
OUTPUT:
4. The Conditional Operators:
operator |
name |
&& |
Conditional-AND |
|| |
Conditional-OR |
?: |
Ternary operator |
The && and || operators perform Conditional-AND(&&) and Conditional-OR(||) operations on two boolean expressions whereas the ternary operator is the shorthand of if-then-else-statement
the syntax for the ternary operator:
variable x = (expression) ? value if true : value if false |
The below example demonstrates the ternary operator
OUTPUT:
In the above example, the result variable is assigned with a true condition so it prints the value of x whereas reuse is assigned with a false statement so it returns the y value
5. Bitwise and BitShift Operators
operator |
Name |
Description |
~ |
Unary bitwise complement |
compliment operator inverts a bit of pattern |
<< |
Signed left shift |
left shift operator that moves the bits to the left |
>> |
Signed right shift |
right shift operator that moves the bits to the right |
& |
Bitwise AND |
compares corresponding bits of x and y and generates 1 if both bits are equal else it returns 0. |
^ |
Bitwise exclusive OR |
compares corresponding bits of x and y and generates 1 if they are not equal else it returns 0. |
| |
Bitwise inclusive OR |
compares corresponding bits of x and y and generates 1 if either bit is 1 else it returns 0. |
Program:
OUTPUT: