What is an Arithmetic Expression? Write the procedure for evaluating an Arithmetic Expression?

An arithmetic expression is a combination of variables constants , and operators arranged as per the syntax of the language . C can handle any complex mathematical expressions.

Ex:–   x = 3x2+4y+z
           x = 3*x*x+4*y+z;

Evaluation of expressions:

expressions are evaluated using as assignment statement of the form:

variable = exp;

Variable is any valid C variable name. When the statement is encountered, the expression is evaluated first & the result then replaces the previous value of the variable on the left hand side.

All the variables used in the expression must be assigned values before evaluation is attempted.

Ex:– z = a–b/c+d;

What this statement is used in a program, the variables a, b, c & d must be defined before they are used in the expression.

Precedence of Arithmetic Operators:–

An arithmetic expression without parenthesis will be evaluated from left to right using the rules of precedence of operators.

There are two distinct priority levels of arithmetic operators in C:
High priority  * / %
Low priority  + –

The basic evaluation procedure includes two left to right passes through the expression:

®During the 1st pass, the high priority op.s are applied as they are encountered.

®During the 2nd pass, the low priority ops are applied as they  are encountered.

Ex:–

x = a–b/3+c*2–1

When a = 9, b =12 &c = 3, the statement becomes

x=9–12/3+3*2–1   and is evaluated as follows.

First pass:

Step1: x = 9–4+3*2–1

Step2:x = 9–4+6–1

Second pass:

Step3: x = 5+6–1

Step4: x = 11–1

Step5: x = 10

Rules for evaluation of exp:–

1. 1st parenthesized sub expression from left to right are evaluated.

2. If parenthesis are nested, the evaluation begins with inner most sub expression.

3. The precedence rule is applied in determining the order of applied of operators  in evaluating sub expressions.

4. The associativity rule is applied when two or more ops of  the same precedence level appear in a sub expression.

5. arithmetic expressions are evaluated from left to right using the rules of precedence.

6. When parentheses are used, the expressions within parenthesis assume highest priority.

Related

C Language 5140887572148250531

Post a Comment

emo-but-icon

item