Explain in detail about expression evaluation

Estudies4you

 Evaluation of Expressions, Type Conversions

Q21. Explain in detail about expression evaluation.
Answer :

Evaluation of Expression
Expressions can be evaluated using assignment statements.
Syntax  
Variables = Expression
Variable indicates the name of the variable. First the expression is evaluated and then the result of it is replaced with the existing value of the variable that is present on the left-hand side. Each variable must be assigned with values before performing the evaluation. Consider the following example.
Example
Consider,
a = 9, b = 12, c = 3
x = a-b/3 + c*2 - 1;
Then the evaluation of expression is as follows,
First Pass
               Step 1:  x = 9 - 12/3 +3*2 - 1
               Step 2:  x = 9 - 4 + 6 -1
Second Pass
               Step 3: x = 5 + 6 -1
               Step 4: x = 11-1
               Step 5: x = 10
The variables a, b, c when used in the program must be defined first before using them in the expressions.
Example :
#include<iostream.h>
#include<conio.h>
main() :
{
float a,b,c,x,y,z;
clrscr();
a = 9;
b = 12;
c = 3; 
cout<<“Evaluation of expressions:”<<endl;
x = a - b/3 + c* 2 - 1;
y = a - b/(3+c)*(2 - 1);
z = a - (b/(3+c)* 2) - 1;
cout<<"“x ="<<x<<endl;
cout<<"y ="<<y<<endl;
cout<<“z ="<<z<<endl;
getch( );
return 0;

}

Output


To Top