Operators
An Operator is a function that acts on two or more values and returns a value. The most commonly used operators are special symbols like + for addition, - for subtraction. The variables that an operator acts on are called operands.
Example:
x+y = 30;
In the above example, + is the operator that adds the two operands x and y together.
Types of Operators
Arithmetic Operators
An arithmetic operator operates on two numeric values and returns a single numeric value.
Syntax
<op1> <Operator> <op2>
Where <op1> and <op2> are numeric expressions.
Operator | Functionality |
+ | addition of <op1> and <op2> |
- | subtracts <op2> from <op1> |
* | multiplies <op1> and <op2> |
/ | divides <op1> by <op2> |
% | remainder when dividing <op1> by <op2> |
Relational Operators
A relational operator compares two values and returns a Boolean expression (either true or false) depending on the two values' relationship.
Example
5 > 4 returns true.
233.6 < 94.22 returns false.
Syntax
<op1> <Operator> <op2>
Operator | Functionality |
> | <op1> is greater than <op2> |
>= | <op1> is greater than or equal to <op2> |
< | <op1> is less than <op2> |
<= | <op1> is less than or equal to <op2> |
== | <op1> is equal to <op2> |
!= | <op1> is not equal to <op2> |
Conditional Operators
A conditional operator operates on a Boolean expression. You can write complex decision logic by combining relational operators and conditional operators.
Syntax
<boolean expression> && <boolean expression>
<boolean expression> || <boolean expression>
! <boolean expression>
Operator | Functionality |
&& | Both the left and right boolean expressions are true |
ll | Atleast one of the boolean expression is true |
! | boolean expression is false |