CodeClerks

Special Assignment Expressions



Write the reason you're deleting this FAQ

Special Assignment Expressions

ChainedAssignment

x = (y = 10);

or

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



EmbeddedAssignment

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;

Comments

Please login or sign up to leave a comment

Join