Programming Fundamentals

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


Operators
Meaning
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Reminder
^
Exponent

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

Operators
Meaning
<
Less Than
>
Greater Than
=
Equal To
!=
Not Equal To
<=
Less Than Equal To
>=
Greater Than Equal To

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

Operators
Meaning
&&
Conjunction
||
Disjunction
!
Negation

When these operators are used in an expression the output is obtained to the following table.

X
Y
X && Y
TRUE
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
TRUE
FALSE
FALSE
FALSE
FALSE

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.

X
Y
X || Y
TRUE
TRUE
TRUE
TRUE
FALSE
TRUE
FALSE
TRUE
TRUE
FALSE
FALSE
FALSE

OR(||) Table

As per above table OR(||) returns true, if any one of the input is true.

X
!X
TRUE
FALSE
FALSE
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.

Mathematical
Programming
AB
C
A*B/C
A x B
A*B
A+B x C
A+B*C
A(B+5)
A* (B+5)

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.

Operators
Order
()
1
^,  NOT(!)
2
/, *, AND(&&)
3
+, -, OR(||)
4