0% found this document useful (0 votes)
68 views10 pages

What Is An Expression?: Expressions

An expression is a combination of operators and operands that represents a specific value. There are three types of expressions: infix, postfix, and prefix. In infix expressions the operator is between the operands, in postfix the operator comes after the operands, and in prefix the operator comes before the operands. Expressions can be converted between these types using procedures that change the order of operators and operands based on operator precedence. Postfix expressions can be evaluated using a stack data structure by pushing operands and popping to perform operations.

Uploaded by

shemse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views10 pages

What Is An Expression?: Expressions

An expression is a combination of operators and operands that represents a specific value. There are three types of expressions: infix, postfix, and prefix. In infix expressions the operator is between the operands, in postfix the operator comes after the operands, and in prefix the operator comes before the operands. Expressions can be converted between these types using procedures that change the order of operators and operands based on operator precedence. Postfix expressions can be evaluated using a stack data structure by pushing operands and popping to perform operations.

Uploaded by

shemse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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.

An expression can be defined as follows...

An expression is a collection of operators and operands that represents


a specific value.

In above definition, operator is a symbol which performs a particular task


like arithmetic operation or logical operation or conditional operation
etc.,

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.

The general structure of an Infix expression is as follows...

Operand1 Operator Operand2

Example

Postfix Expression
In postfix expression, operator is used after operands. We can say that
"Operator follows the Operands".

The general structure of Postfix expression is as follows...

Operand1 Operand2 Operator

Example

Prefix Expression
In prefix expression, operator is used before operands. We can say that
"Operands follows the Operator".

The general structure of Prefix expression is as follows...

Operator Operand1 Operand2

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

Any expression can be represented using three types of expressions


(Infix, Postfix and Prefix). We can also convert one type of expression to
another type of expression like Infix to Postfix, Infix to Prefix, Postfix to
Prefix and vice versa.

To convert any Infix expression into Postfix or Prefix expression we can


use the following procedure...
1. Find all the operators in the given Infix Expression.
2. Find the order of operators evaluated according to their Operator
precedence.
3. Convert each operator into required type of expression (Postfix or
Prefix) in the same order.

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*+ =

Finally, given Infix Expression is converted into Postfix Expression as


follows...

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-*

Postfix Expression Evaluation

A postfix expression is a collection of operators and operands in which


the operator is placed after the operands. That means, in a postfix
expression the operator follows the operands.

Postfix Expression has following general structure...

Operand1 Operand2 Operator


Example

Postfix Expression Evaluation using Stack


Data Structure
A postfix expression can be evaluated using the Stack data structure. To
evaluate a postfix expression using Stack data structure we can use the
following steps...

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...

You might also like