views:

164

answers:

3
+2  Q: 

Infix vs Postfix

Had this question in the interview yesterday. Which is better to use? Infix(with parenthesis) or Postfix? State with reason..

I only could tell them that:

  1. it is easier for the compilers to process postfix expression for arithmetic evaluations and operator precedence.
  2. More memory is used for storing and processing the parenthesis.

Please throw some light on whether I am right on this?

+1  A: 

Memory is not usually an issue here. Compilers spend tons of memory on other things. Postfix usually entails an operand stack, which involves a certain amount of memory (still small) at runtime.

Postfix is useful for low-level interpretable code, such as Java, Forth, Postscript. These all have hardware or super-lightweight software implementations.

Infix is better for readability.

Of course, there's no way to know how the issue affects your field without knowing what kind of job it is.

Potatoswatter
+5  A: 

Postfix doesn't require any order of operations; it's always explicit. So for a stack-based compiler, it's very easy to implement, and for humans, it's easy to understand the order of operations.

On the other hand, infix doesn't require you to read all the verbs at the end :-P. Humans work on infix or even prefix; "add A to B" makes more sense than "A and B: add them".

But, this question is fundamentally subjective. Postfix will always be better for the computer system, but infix has its merits.

Borealid
A: 

Postfix is better because it explicitly tells what the expression has to do....and we have no need to check parenthesis as well...