Operators & Expressions
Operators:
Operators are special character which perform some operation. Operators are categorized into two major types based on its function and number of operands.
According to its function we can classify operators into three different type. they are
1. Arithmetic
2. Relational
3. Logical
According to number of operands operators are classified again into three types.
1. Unary
2. Binary
3. Ternary
Arithmetic operators:
These are operators which are used to calculate and evaluate arithmetic expressions. these includes
all these operators also come under the category of binary operators. They all takes 2 operands.
Relational operators:
These are used to compare two numerical quantities or strings. For example we can check whether a number is positive or negative or less than a particular value and so on. These operations return either True or False. Some of the operators are
These operators are also binary operators and it combines two variables or values. It is written in between two operands.
Logical operators:
These operators are used to perform logical operations on numeric and string values. It is used mainly with relational expressions. The operator gives only true or false as the output. the common operators are
When these operators are used in an expression the output is obtained to the following table.
AND(&&) Table
After analyzing the above table we can conclude that when both the input are true the output is true otherwise the output is false.
OR(||) Table
As per above table OR(||) returns true, if any one of the input is true.
NOT(!) Table
NOT operators return False if the input is True and return True if the input is False
Among the logical operators AND(&&) , OR(||) are binary operators whereas NOT(!) is a unary operators, since it takes only one operands.
Expressions:
Expression are statement which are constructed by variables, constants and operators. The expressions are written little differently than the normal way.
They are classified into three types:
1. Arithmetic
2. Relational
3. Logical
Arithmetic expression:
these expression are formed only by arithmetic operators. It is used to solve mathematical expression or equations. The following example shows how arithmetic expression are written.
1. x+y-z
2. c=f*23+56
3. d=(F/23)*(x^2)
3. g=M%5
The expression which are written mathematically are written in programming and their differences are shown bellow.
Relational expressions:
These expression use only relational operators. These are basically used in checking. The following are some of the examples of relational expressions.
1. A
2. A>B
3. 4=5
4. A+B >= B-5
Logical expressions:
These expression are generally made along with relational expressions. It is used to combine more than two relational expressions together.
1. A
2. A=b || B=6
3. !B
Hierarchy of operators:
The operators are assigned priorities in order to ease job of evaluation. The following table shows the hierarchy.