Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Expressions and

Evaluation
of Expressions,
Type Conversions

CSE 1001 Department of CSE 6/22/2015 1


Objectives

To learn and appreciate the following concepts

syntax of expressions
evaluation of expressions
Type conversion (automatic)
syntax of type casting(explicit conversion)

CSE 1001 Department of CSE 6/22/2015 2


Session outcome
At the end of session one will be able to

Understand type conversions


Evaluate expressions

CSE 1001 Department of CSE 6/22/2015 3


Expression
Anything whose evaluation yields a numerical
value is called an expression.
More complex expression use operators
(combining the simple expressions).
Example:
1.25 / 8 + 5 * rate + rate * rate / cost;
x = 2 + 8;
x = a + 10;
The last one evaluates the expression a + 10 and
assigns the result to variable x.

CSE 1001 Department of CSE 6/22/2015 4


Expression
The general form of expression and
variables, evaluated from the right to left,
is:
variable = any_expression;

Example: y = x = a + 10;
x = 6 + (y = 4 + 5);

CSE 1001 Department of CSE 6/22/2015 5


Examples of Expressions

Algebraic expression C++ expression

a X b c a*bc

(m + n) (x + y) (m + n) * (x +y)

ab
a*b / c
c
x
c x/y + c
y

3x 2 2 x 1 3*x*x+2*x+1

CSE 1001 Department of CSE 6/22/2015 6


Type Conversion
C++ permits mixing of constants and variables of
different types in an expression.
But during evaluation it adheres to very strict rules of
type conversion.
We know that the computer considers one operator at
a time, involving two operands. If the operands are of
different types, the lower type is automatically
converted to the higher type before the operation
proceeds. The result is of higher type.
Finally result of an expression is converted to the type
of the variable on the left of the assignment sign.(for
eg. float to int causes truncation of the fractional part)

CSE 1001 Department of CSE 6/22/2015 7


Type Conversion
Thus while storing the value, the expression always
takes the type of the variable into which its value is
being stored.
Lets see another example:
float a,b,c;
int s;
s= a * b * c / 100 + 32 / 4 - 3 * 1.1;

During evaluation of the expression all the ints would


be promoted to floats and the result of the expression
would be a float.
But when this float value is assigned to s it is demoted
to an int and then stored in s.
CSE 1001 Department of CSE 6/22/2015 8
Type Conversion

C++ performs type conversions automatically.


Example:
int i, x;
float f;
double d;
long int l;

x=l/i +I*fd;

CSE 1001 Department of CSE 6/22/2015 9


Type Conversion
However, there are instances when we want to force a
type conversion in a way that is different from the
automatic conversion.

Consider, for example, the calculation of ratio of doctors


to engineers in a town
Ratio = doctor_number / engineer _number

float Ratio; int doctor_number=33,


engineer_number=66;
Ratio = doctor_number / engineer _number; // Ratio = 0.0

Ratio = (float) doctor_number / engineer _number; // Ratio = 0.5

CSE 1001 Department of CSE 6/22/2015 10


Casting a value
The process of such a local conversion is
called as casting a value.
Syntax:
(type-name) expression;
where type-name is one of the
standard C++ data type.

CSE 1001 Department of CSE 6/22/2015 11


Type Casting
Some times we need to force the compiler to
explicitly convert the value of an expression to a
particular data type.
Eg: float a; int x=6, y=4;
a=x / y;
cout<<a;
The value of a from the above code snippet is 1.0
and not 1.5.
This is because 6 and 4 are integers and hence 6/4
yields an integer 1. This 1 when stored in a is converted
to 1.0

But what if we dont want the quotient to be truncated?

CSE 1001 Department of CSE 6/22/2015 12


Type Casting
One solution is to make either x or y as float.
Let us suppose the other requirements of the program
does not permit us to do so.
In such a case we use Type Casting.

Let us see the previous code snippet once again with a


small modification
float a; int x=6,y=4;
a = (float) x / y;
cout<<a;

Here the value of a is 1.5

CSE 1001 Department of CSE 6/22/2015 13


Examples
int sum=20,n=3,x=5;
float a=3.0,b=2.0;
cout<<float(5/3)+3/5; 1
cout<<(int)7.5; 7
cout<<(int)21.3/(int)4.5;
5
cout<<(double)sum/n; 6.666667
cout<<(int)(a+b); 5
cout<<(int)a+b; 5
cout<<cos((double)x); 0.283622
cout<<(float)(5/3+3/5); 1

CSE 1001 Department of CSE 6/22/2015 14


Summary

Expressions and evaluation of


MCQs
expressions
Type conversion-Casting a value

CSE 1001 Department of CSE 6/22/2015 15

You might also like