Page 28 - DCAP404 _Object Oriented Programming
P. 28
Unit 2: Beginning of OOP Language
2.2 Assignment Expression Notes
The expression that makes uses of assignment operations is called as in assignment expression.
Assignment expression assigns the value of an expression, a constant or a variable to an
identifier. Assignment expressions are often referred to as assignment statements, since they are
usually written as complete statement. The rules for writing assignment expressions are as
follows:
1. Don’t confuse the assignment (=) with the relational operator (==). The assignment operator
is used to assign a value to an identifier, where as the equality operator is used to determine
if two expressions have the same value. This,
a = 1
and
a == 1
are two different expressions.
2. The sign (=) equal to is an operator and not an equation maker, so it can appear anywhere
in place of another operator. The following are legal assignment expressions.
a = b = c + 4;
a = 2 * (b = 10/c);
C++ allows to use of multiple assignment operators in a series, for example,
a = b = c = 2;
In such situation, the assignments are carried out from right to left. There are, however,
restrictions to the level to which these assignments can be chained. For instance, Turbo
C++ allows to chain maximum 70 assignment operators in a statement i.e.
v1 = v2 = v3 = ………………… = v70 = 10;
where v1, v2, v3 are variable of similar type and are assumed to be pre declared. When
executed the value 10 will be assign to v70 and the value of v70 will be assign to v69 and
so on. Finally the variable v1 will be assigned with the value 10 as the value assignment
operation carried out from right to left.
3. With the compound assignment operators, compiler automatically supplies parentheses -
explicit parenthesis around the expression which is entirely superfluous. For example, the
following statement:
a + = b + 1;
is required to
a = a + (b+1);
4. If the two operands in an assignment expression are of different data types, then the value
of the expression on the right will automatically be converted to the type of the variable
on the left.
5. Assignment operators have a lower precedence than any of the other operators. Therefore,
various operations like unary operations, arithmetic operations, relational operations,
equality operations and logical operations are all carried out before assignment
operations.
LOVELY PROFESSIONAL UNIVERSITY 21