In this section we will learn about various Operators. An operator is a symbol which performs for specific mathematical or logical operation. For an example, plus (+) operator is used for addition and minus (-) operator is used for subtraction.
Here we will talk about the following operators
- Arithmetic Operators
- Increment / Decrement Operators
- Assignment Operators
- Relational Operators
- Logical Operators
Arithmetic Operators
Arithmetic Operators are used to perform mathematical operation like addition, subtraction, division and multiplication. Following table shows the arithmetic operators with example. Let, X = 5 and Y = 2
Operator | Operation | Description | Example |
---|---|---|---|
+ | Addition | Adds two operands | X + Y = 7 |
- | Subtraction | Subtracts second operand from the first | X - Y = 3 |
* | Multiplication | Multiplies both operands. | X * Y = 10 |
/ | Division | Divides numerator by denominator. | X / Y = 2.5 |
% | Modulus | Gives remainder value after divides the first operands by second operands | X % Y = 1 |
Increment / Decrement Operators
Increment (++) Operator increases value by one and Decrement (--) Operator decreases value by one. Following table shows Increment / Decrement operators with example. Let, X = 5 and Y = 2
Operator | Operation | Description | Example |
---|---|---|---|
++ | Increment | increases value by one | X++ is equal to 6 |
-- | Decrement | decreases value by one | Y-- is equal to 1 |
Assignment Operator
Assignment Operators are used to assign value into variables. Following table shows the Assignment operators with example.
Operator | Description | Example | Same As |
---|---|---|---|
= | Assigns values from right side operands to left side operand | X = Y | X = Y |
+= | Add and assignment operator | X += Y | X = X + Y |
-= | Subtract and assignment operator | X -= Y | X = X - Y |
*= | Multiply and assignment operator | X *= Y | X = X * Y |
/= | Divide and assignment operator | X /= Y | X = X / Y |
%= | Modulus and assignment operator | X %= Y | X = X % Y |
Relational Operators
Relational Operators are used to compare between two operands. Following table shows the Relational operators with example. Let, X = 5 and Y = 2
Operator | Description | Example | True/False |
---|---|---|---|
== | Is Equal to | X == Y. | (5 == 2) False |
!= | Is Not Equal to | X != Y | (5 != 2) True |
> | Greater than | X > Y | (5 > 2) True |
< | Less than | X < Y | (5 < 2) False |
>= | Greater than or Equal to | X >= Y | (5 >= 2) True |
<= | Less than or Equal to | X <= Y | (5 <= 2) False |
Logical Operators
Logical Operators are used in decision making. Following table shows the Logical operators with example. let X = True and Y = False
Operator | Description | Example | True/False |
---|---|---|---|
&& | Checks both operands are True | (X && Y) | False |
|| | At least one of the Operands is True | (X || Y) | True |
! | It checks the reverse condition is true or not | !(X) | False |
No comments:
Post a Comment