Special Assignment Expressions

Write the reason you're deleting this FAQ

Special Assignment Expressions


x = (y = 10);


x = y = 10;

First10 is assigned to y and then to x.

A chained statement can not be used to initialize variables at the time of declaration. For instance, the statement

float a = b = 12.34; // wrong

is illegal. This may be written as

float a = 12.34, b = 12.34 // correct


X = (y = 50) + 10;

(y= 50) is an assignment expression known as embedded assignment. Here, the value 50 is assigned to y and then the result 50+10 = 60 is assigned to x. This statement is identical to

x = 50;

x = y + 10;

Compound Assignment

LikeC, C++ supports a compound assignment operator which is a combination of the assignment operator with a binary arithmetic operator. For example, the simple assignment statement

x = x +10;

maybe written as

x + = 10;

Theoperator += is known as compound assignment operator or short-hand assignment
operator. The general form of the compound assignment operator is:

variable1 op= variable2;

Whereop is a binary arithmetic operator. This means that

Variable1 = variable1 op variable2;


Please login or sign up to leave a comment