LECTURE - 7 - DR - Shada
LECTURE - 7 - DR - Shada
Expressions
Dr.Shada Mabger
Infix, Postfix and Prefix notations
• The way to write arithmetic expression is
known as a notation.
• An arithmetic expression can be written in
three different but equivalent notations :
1. Infix Notation
2. Prefix Notation
3. Postfix Notation
٢
Infix Notation
• We write expression in infix notation:
e.g. :
a-b+c
• where operators are used in-between operands.
• It is easy for us humans to read, write, and speak
in infix notation but the same does not go well
with computing devices.
• An algorithm to process infix notation could be
difficult and costly in terms of time and space
consumption.
٣
Infix Notation
٤
Infix Notation
• We can see that the Operator is sandwiched in
between the Operands for the Infix notation.
• This is true for any mathematical formulae which
uses Infix notation:
Example :
2+3
5—2
and
2+3+4
2+5/2
1+2/3
• when we mix Operands we have a precedence
(that is, order of operations)issue .
٥
Prefix Notation
• Prefix notation puts the operator before all of
the operand .
• For example, +ab.
• This is equivalent to its infix notation a + b.
The Prefix Example of : 2+3
Infix: 2 + 3
Prefix: + 2 3
٦
Prefix Notation
Example :
1- Infix: 2 + 3 + 4
Prefix: ++234
2- Infix :A+B+C+D
Prefix : +++ABCD
3-Infix : A*B+C/D
Prefix : +*AB/CD
٧
Postfix Notation
• Puts the operator before all of the operands.
• This is just like the prefix notation, but the
operand comes at the end of the expression,
and similarly removes the need for brackets
within any expressions.
Example :
Infix: 2 + 3
Postfix: 2 3 +
٨
Postfix Notation
Example :
1- Infix: 2 + 3 + 4
Postfix: 23+4+
2- Infix :A+B+C+D
Postfix : AB+C+D+
3-Infix : A*B+C/D
Postfix : AB*CD/+
٩
Precedence
• As multiplication operation has precedence
over addition, b * c will be evaluated first. A
table of operator precedencAs multiplication
١٠
Associativity
• Describes the rule where operators with the
same precedence appear in an expression.
• For example, in expression a + b − c ,
• both + and – have the same precedence, then
which part of the expression will be evaluated
first, is determined by associativity of those
operators.
• Here, both + and − are left associative, so the
expression will be evaluated as (a + b) − c.
١١
Table of Operator Precedence
١٢
Examples of Infix, Prefix, and Postfix
١٣