Introduction

Evaluating an expression is one of the most common tasks in programming. For example a+b+c is an expression. Here + is an operator. The variables a, b and c are operands. The value of the expression is the sum of a, b and c. An expression may be often evaluated to assign a variable a new value or to verify the truthfulness of a test condition. For example the operator can be unary(Ex: ~,++ etc.), binary(Ex: +,* etc.) or ternary (Ex: (a>b)?p:q)

Examples of expressions:

1. To compute the area of a triangle from base b and height h
Area=1/2.0*base*height
Note that this expression will be evaluated left to right, that is the division opeartor will be evaluated, followed by two multiplication operators. The 2.0 is used to make sure that the division is carried out as a floating point operation.
2. To compute the area of a triangle from the three sides, a, b and c
s=(a+b+c)/2.0
area = s*(s-a)*(s-b)*(s-c)
Here area is calculated in two steps. The first step, s is computed from the three sides a, b and c. Note that, the paranthesis is used so that evaluation within the paranthesis takes first. Without paranthesis, we would have obtained the value of s as sum of a, b and half of c. Similarly, paranthesis are very important in evaluating the second expression.
3. To find the nth term of an arithmetic progression, we can use the following expression
tn= a+(n-1)*d
where a is the first term and d is the common difference.
Here also, the use of paranthesis has helped us in evaluating the expression the way we need it.

An expression used in a computer program is very similar to an algebraic expression and when many operators are specified in an expression, then the evaluation takes place according to the precedence of operators.The order of the precedence is given by a table of precedence.

A paranthesis can be used to override the precedence of operators and force the evalutation of a sub-expression within an expression. Expression is evaluated according to assosciativity of the operator. The assosciativity of the operator is a property that determines how operators of the same precedence are grouped in the absence of paranthesis. The objective in evaluating an expression is to consider the precedence and aasosciativity of the individual operators and compute the value of the sub-expressions.

Theory

Evaluating an expression involves repeatedly solving the sub-expressions in the lowest level paranthesis and substituting its value to solve the bigger expression. If an expression or sub-expression does not have a paranthesis, then we can directly compute its value based on the precedence and assosciativity of the operators present in the expression. That is, the sub expression involving operator with higher precedence is evaluated before other sub-expressions. The following table gives the precedence order of all the operators:

Operators can be broadly categorized as:

1. Arithmetic Operators: These operators are used for arithmetic operations like addition,subtraction and multiplication. Ex: DIVISION(/),MODULUS OR REMAINDER (%)
2. Relational Operators: These operators are typically used for comparison of two operands.Ex: GREATER THAN(>),GREATER THAN OR EQUAL TO(>=) etc.
3. Logical Operators: These operators are used for conducting logical operations like AND,OR. Ex: LOGICAL AND( && ),LOGICAL OR (||) etc.
4. Bitwise Operators: These operators are used to perform operations on bits. Ex: BITWISE AND( & ), BITWISE OR ( | ) etc.

Objective

1. To learn about different types of operators.
2. To learn about the precedence of the operators.
3. To learn how to evaluate an expression.

Manual

Here we shall see the step by step evaluation of mathematical expressions.

Procedure

Procedure for the experiment is as follows.

1. Select the type of operators and the datatype you want to work upon from the top most bar.
2. You can edit the values of variables by pressing the edit button.
3. Select an expression prototype from the menu.
4. You can also edit this expression.
5. You can also edit this expression.
6. Press Next to see the step by step evaluation of the selected expression in the central panel and the corresponding reasoning in the right panel.
7. Press stop if you want to abort the experiment and start over.