You are here:

by Dinesh Thakur
Category: Object-oriented programming (OOP)

A combination of variables, constants and operators that represents a computation forms an expression. Depending upon the type of operands involved in an expression or the result obtained after evaluating expression, there are different categories of an expression. These categories of an expression are discussed here.

• Constant expressions: The expressions that comprise only constant values are called constant expressions. Some examples of constant expressions are 20, *‘ *a*‘ * and 2/5+30 .

**• Integral expressions: **The expressions that produce an integer value as output after performing all types of conversions are called **integral expressions. **For example, x, 6*x-y and 10 +int (5.0) are integral expressions. Here, x and yare variables of type into

**• Float expressions: **The expressions that produce floating-point value as output after performing all types of conversions are called **float expressions. **For example, 9.25, x-y and 9+ float (7) are float expressions. Here, x 'and yare variables of type float.

**• Relational or Boolean expressions: **The expressions that produce a bool type value, that is, either true or false are called **relational or Boolean expressions. **For example, x + y<100, m + n==a-b and a>=b + c .are relational expressions.

**• Logical expressions: **The expressions that produce a bool type value after combining two or more relational expressions are called **logical expressions. **For example, x==5 &&m==5 and y>x I I m<=n are logical expressions.

**• Bitwise expressions: **The expressions which manipulate data at bit level are called **bitwise expressions. **For example, a >> 4 and b<< 2 are bitwise expressions.

**• Pointer expressions: **The expressions that give address values as output are called **pointer** **expressions. **For example, &x, ptr and -ptr are pointer expressions. Here, x is a variable of any type and ptr is a pointer.

**• Special assignment expressions: **An expression can be categorized further depending upon the way the values are assigned to the variables.

** •** **Chained assignment: Chained assignment **is an assignment expression in which the same value is assigned to more than one variable, using a single statement. For example, consider these statements.

**a = (b=20); or a=b=20;**

** **

In these statements, value 20 is assigned to variable b and then to variable a. Note that variables cannot be initialized at the time of declaration using chained assignment. For example, consider these statements.

int a=b=30; // illegal

int a=30, int b=30; //valid

** •** **Embedded assignment: Embedded assignment **is an assignment expression, which is enclosed within another assignment expression. For example, consider this statement

a=20+(b=30); //equivalent to b=30; a=20+30;

In this statement, the value 30 is assigned to variable b and then the result of (20+ 30) that is, 50 is assigned to variable a. Note that the expression (b=30) is an embedded assignment.

**Compound Assignment: Compound Assignment** is an assignment expression, which uses a compound assignment operator that is a combination of the assignment operator with a binary arithmetic operator. For example, consider this statement.

a + =20; //equivalent to a=a+20;

In this statement, the operator += is a compound assignment operator, also known as short-hand assignment operator.

An expression· may involve variables and constants either of same data type or of different data types. However, when an expression consists of mixed data types then they are converted to the same type while evaluation, to avoid compatibility issues. This is accomplished by type conversion, which is defined as the process of converting one predefined data type into another. Type conversions are of two types, namely, *implicit* *conversions *and *explicit conversions *also known as *typecasting.*

Implicit conversion, also known as automatic type conversion refers to the type conversion that is automatically performed by the compiler. Whenever compiler confronts a mixed type expression, first of all char and short int values are converted to int. This conversion is known as integral promotion. After applying this conversion, all the other operands are converted to the type of the largest operand and the result is of the type of the largest operand. Table 3.8 illustrates the implicit conversion of data type starting from the smallest to largest data type. For example, in expression 5 + 4.25, the compiler converts the int into float as float is larger than int and then performs the addition.

Typecasting refers to the type conversion that is performed explicitly using type cast operator. In C++, typecasting can be performed by using two different forms which are given here.

**data_type (expression) //expression in parentheses**

** (data_type)expression //data type in parentheses**

where,

data_type = data type (also known as *cast operator) *to which the expression is to be converted.

To understand typecasting, consider this example.

** float (num)****+ 3.5; //num is of int type**

In this example, float () acts as a conversion function which converts int to float. However, this form of conversion cannot be used in some situations. For example, consider this statement.

**ptr=int * (x) ***;*

In such cases, conversion can be done using the second form of typecasting (which is basically C-style typecasting) as shown here.

** ptr=(int*)x;**

Dinesh Thakur is a Technology Columinist and founder of Computer Notes and Technology Motivation.
Turn Mobile View Off