Understanding Expressions in Computer Science and Mathematics: Infix, Prefix, and Postfix Notation

Andika Wirawan
4 min readJul 6, 2023

Expressions are combinations of symbols that represent values ​​or calculations. For example, 2 + 3 is an expression that represents the value 5, and x * y is an expression that represents the calculation of multiplying x and y.

Expressions can be written in different ways, depending on how the symbols are arranged and sequenced. The most common way is infix, prefix and postfix notation.

Infix Notation
Parenthetical notation is a common way of writing expressions. In infix notation, operators are placed between operands. For example, 2 + 3 and x * y are written in infix notation.

Parenthetical notation is easy for humans to read and write, but can be ambiguous or complex for computers. For example, the expression 2 + 3 * 4 can have different meanings depending on the order in which they are operated. To avoid ambiguity, infix notation often uses parentheses to indicate precedents and operator associations. For example, (2 + 3) * 4 and 2 + (3 * 4) are unambiguous expressions in infix notation.

Prefix Notation
Prefix notation is a way of writing expressions that places operators before operands. For example, + 2 3 and * x y are written with prefix notation.

Prefix notation is also known as Polish notation, after the Polish mathematician Jan Łukasiewicz who invented it in the 1920s. Prefix notation has the advantage of being unambiguous and easy to decipher for computers. No need for brackets or rules for the order of operations. You can evaluate prefix expressions by scanning them from left to right and applying a stack data structure.

For example, to evaluate the prefix expression + * 2 3 4, we can use the following steps:

  • Scan the expression from left to right.
  • If we find an operator, push it to the stack.
  • If we find an operand, take the top operator from the stack and apply it to the operand and the next operand on the right.
  • Push the results back onto the stack.
  • Repeat until the expression runs out.
  • The end result is at the top of the heap.

These steps can be explained as follows:

| Expression | Stack | Result |
| — — — — — — | — — — -| — — — — |
| + * 2 3 4 | | |
| * 2 3 4 | + | |
| 2 3 4 | + * | |
| 3 4 | + * 2 | |
| 4 | + * 2 3 | |
| | + * 2 (3 * 4) | |
| | + (2 * (3 * 4)) | |
| | (2 * (3 * 4)) + | |
| | ((2 * (3 * 4)) +) | |

The final result is ((2 * (3 * 4)) +), which equals 26.

Postfix Notation
Postfix notation is a way of writing expressions that places an operator after the operand. For example, 2 3 + and x y * are written in postfix notation.

Postfix notation is also known as reverse Polish notation, as it is the reverse of prefix notation. Postfix notation has the same advantages as prefix notation: it’s unambiguous and easy to parse for computers. It also doesn’t require brackets or rules for the order of operations. Evaluation of postfix expressions can be done by scanning them from left to right and applying the stack data structure.

For example, to evaluate the postfix expression 2 3 * 4 +, we can use the following steps:

  • Scan the expression from left to right.
  • If we find a gap, push it into the pile.
  • If we find an operator, remove the top two operands from the stack and apply the operator to them.
  • Push the results back onto the stack.
  • Repeat until the expression runs out.
  • The end result is at the top of the heap.

These steps can be explained as follows:

| Expression | Stack | Result |
| — — — — — — | — — — -| — — — — |
| 2 3 * 4 + | | |
| 3 * 4 + | 2 | |
| * 4 + | 2 3 | |
| 4 + | (2 * 3) | |
| + | (2 * 3) 4 | |
| | ((2 * 3) +) (4) | |
| | ((2 * (3)) + (4)) | |
| | (((2) * (3)) + (4)) | |

The final result is (((2) * (3)) + (4)), which equals 10.

These are some definitions and examples of expressions, infixes, prefixes, and postfix notation in computer science and mathematics. This notation can be useful for different purposes and applications, such as arithmetic, logic, programming, etc. This notation can also help us understand and compare different ways of representing and manipulating data and calculations.

--

--

Andika Wirawan

I am Andika Wirawan, welcome. I work as a freelancer and enjoy reading and writing. I appreciate you reading my writing.