...

/

Infix, Prefix, and Postfix Expressions

Infix, Prefix, and Postfix Expressions

Let’s discuss infix, prefix, and postfix expressions.

Infix expression

When we have an algebraic expression like A + B, we know that variable A is added to variable B. This type of expression is called an infix expression because the operator + is between operand A and operand B.

Ambiguous expressions

Let’s consider another infix expression, A + B * C. In the expression, there is the problem of operator precedence. What operation will be performed first: + or *? Are A and B added first, and then the result is multiplied with C? Alternatively, B and C are multiplied first, and then the result is added to A. This makes the expression ambiguous.

To deal with this ambiguity, we define the precedence rule or use parentheses to remove ambiguity.

So, if we want to multiply B and C first and then add the result to A, then the same expression can be written unambiguously using parentheses as A + (B * C). On the other hand, if we want to add A and B first and then the multiply the sum with C, we’ll write it as (A + B) * C. Therefore, we need parentheses in the infix expression to make the expression unambiguous.

Different types of expressions

Infix expression:

In this notation, we place operators in the middle of the operands.

<Operand>< ...