What Is An Expression?: Expressions
What Is An Expression?: Expressions
What is an Expression?
In any programming language, if we want to perform any calculation or to
frame a condition etc., we use a set of symbols to perform the task.
These set of symbols makes an expression.
Operands are the values on which the operators can perform the task.
Here operand can be a direct value or variable or address of memory
location.
Expression Types
Based on the operator position, expressions are divided into THREE
types. They are as follows...
1. Infix Expression
2. Postfix Expression
3. Prefix Expression
Infix Expression
In infix expression, operator is used in between operands.
Example
Postfix Expression
In postfix expression, operator is used after operands. We can say that
"Operator follows the Operands".
Example
Prefix Expression
In prefix expression, operator is used before operands. We can say that
"Operands follows the Operator".
Example
Any expression can be represented using the above three different types
of expressions. And we can convert an expression from one form to
another form like Infix to Postfix, Infix to Prefix, Prefix to Postfix and
vice versa.
Expression Conversion
Example
Consider the following Infix Expression to be converted into Postfix
Expression...
D=A+B*C
Step 1: The Operators in the given Infix Expression : = , + , *
Step 2: The Order of Operators according to their preference : * ,
+,=
Step 3: Now, convert the first operator * ----- D = A + B C *
Step 4: Convert the next operator + ----- D = A BC* +
Step 5: Convert the next operator = ----- D ABC*+ =
DABC*+=
Infix to Postfix Conversion using Stack
Data Structure
To convert Infix Expression into Postfix Expression using a stack data
structure, We can use the following steps...
1. Read all the symbols one by one from left to right in the given
Infix Expression.
2. If the reading symbol is operand, then directly print it to the
result (Output).
3. If the reading symbol is left parenthesis '(', then Push it on to the
Stack.
4. If the reading symbol is right parenthesis ')', then Pop all the
contents of stack until respective left parenthesis is poped and
print each poped symbol to the result.
5. If the reading symbol is operator (+ , - , * , / etc.,), then Push it
on to the Stack. However, first pop the operators which are
already on the stack that have higher or equal precedence than
current operator and print them to the result.
Example
Consider the following Infix Expression...
(A+B)*(C-D)
The given infix expression can be converted into postfix expression using
Stack data Structure as follows...
The final Postfix Expression is as follows...
AB+CD-*
1. Read all the symbols one by one from left to right in the given
Postfix Expression
2. If the reading symbol is operand, then push it on to the Stack.
3. If the reading symbol is operator (+ , - , * , / etc.,), then perform
TWO pop operations and store the two popped oparands in two
different variables (operand1 and operand2). Then perform
reading symbol operation using operand1 and operand2 and push
result back on to the Stack.
4. Finally! perform a pop operation and display the popped value as
final result.
Example
Consider the following Expression...