How To

How to write Programming Language Arithmetic Expressions

In programming language arithmetic expressions, there are three types of statements depending on there evaluation process.

  • Infix Statements. This is the most general type of expression like as we used in simple mathematics.
  • Prefix Statements
  • Postfix Statements

Some languages supports only one of it, or two of it or all of it. A language like Java supports all of it.

In Java

a+b Infix notation used in addition

++a Prefix notation used in increment by one effected on the same statement

a++ Postfix notation used in increment by one effected on the next statement

Notation

Infix Statements

Following is the infix notation for binary operations. that is, each binary operator is written between its two operands.

a + b

a and b are operands

+ is the operator

If we take a more complex infix statement

((a + b) – (c * d))

You’ll notice there are parenthesis ( ) around a binary operations. Assuming the normal associativity and precedence of the operators +, -, and *, the expression could also be written as

a + b – c * d

Operator precedence and associativity often vary from one language to another. Even in a single language or family of languages, the precedence table may be quite large. In most cases, however, parentheses may be inserted to achieve the desired semantic structure. That means if you are using parenthesis, then there is no need of precedence or associativity structure defined in the language. But if the language doesn’t have any of them, then the usage of parenthesis is a must.

Prefix Statements

If there is no precedence or associativity rules existed, then usage of parenthesis make the statement more huge and complex. That’s where the prefix notation comes into scene. We termed this more specifically as Polish prefix notation. A binary operator is always written in front of its two operands. For example, the expression of above can be written as follows in prefix.

– + a b * c d

To understand this statement just try to identify operands by covering it parenthesis. Each of the parenthesis starts with the operator.

(- (+ a b) (* c d))

We called this way of parenthesis usage as Cambridge prefix notation.

Postfix Statements

Another variant is called Polish postfix notation, in which an operator always follows its operands. Printer control languages like PostScript used this kind of notation. For example, the same expression of above can be written as follows in postfix.

a b + c d *

To understand this statement just try to identify operands by covering it parenthesis. Each of the parenthesis ends with the operator. We called this way of parenthesis usage as Cambridge postfix notation.

((a b +) (c d *) -)

 

 

An advantage of Cambridge notation is that operators like + and – become naturally n-ary. For example

 a + b + c + d

can be written as

(+ a b c d) in prefix or (a b c d +) in postfix

Cambridge prefix notation is used by both Lisp and Scheme programming languages.

Precedence

The value of an expression depends at least in part on the order of evaluation of the operators in the expression.

3 + 4 * 5

If evaluated left to right (the addition first and then the multiplication), the result is 35. If evaluated right to left, the result is 23.

Instead of simply evaluating the operators in an expression from left to right or right to left, mathematicians long ago developed the concept of placing operators in a hierarchy of evaluation priorities and basing the evaluation order of expressions partly on this hierarchy. For example, in mathematics, multiplication is considered to be of higher priority than addition, perhaps due to its higher level of complexity.

If that convention were applied in the previous expression, multiplication would be done first. So the answer should be 23.

Unary operator

Unary operator expressions have a one operand. For example

-a

-7

are unary expression and most of the time they surrounded with parenthesis.

(-a)

(-7)

Following is a unary operator used in a expression.

a + b * (-c) -d

If we write it as following without the parenthesis, then it is illegal

a + b * -c – d

Ruby C based Languages
Highest **  postfix ++, —
unary +, –  prefix ++, –, unary +, –
*, /, %  *, /, %
 Lowest binary +, –  binary +, –

The ** operator is exponentiation. Some languages written as ^ operator. The % modulus operator takes two integer operands and yields the remainder of the first after division by the second. The ++ and — operators of the C-based languages are called increment and decrements.

Associativity

Consider the following expression:

a – b + c – d

If the addition and subtraction operators have the same level of precedence, as they do in programming languages, the precedence rules say nothing about the order of evaluation of the operators in this expression.

When an expression contains two adjacent occurrences of operators with the same level of precedence, the question of which operator is evaluated first is answered by the associativity rules of the language. An operator can have either left or right associativity, meaning that when there are two adjacent operators with the same precedence, the left operator is evaluated first or the right operator is evaluated first, respectively.

Associativity in common languages is left to right, except that the exponentiation operator (when provided) sometimes associates right to left. In the Java expressions the left operator is evaluated first.

Language Associativity Rule
Ruby Left: *, /, +, –
Right: **
C-based languages Left: *, /, %, binary +, binary –
Right: ++, –, unary -, unary +
Ada Left: all except **
Nonassociative: **

So whatever the programming language you are using always learn about its precedence and associativity rules. If you are not sure, then using of parenthesis get you out from lot of troubles.

 

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW

About the author

Sandeep Likhar

Sandeep Likhar from Indore India, Blogger, eBook Designer and Founder of LetsDnD. Having five years of experience in industry as a Digital Publishing Expert and eBook Converter providing services to Authors, Publishers and distributors worldwide. Proficient in converting books into various formats HTML, epub, mobi, word, PDF including all major online platforms iTunes, Kobo, Kindle, Createspace, B&N, Smashwords and more.

Leave a Reply

1 Comment on "How to write Programming Language Arithmetic Expressions"

Notify of
avatar
Sort by:   newest | oldest | most voted
Kendra
Guest

I think the admin of this web site is in fact working hard in support of his web page, because here every stuff is quality based information.

wpDiscuz
DON’T MISS OUT!
Subscribe To Newsletter
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Give it a try, you can unsubscribe anytime.

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW