Assignment Expressions
Previous Topic  Next Topic 

Assignment expressions assign value to a variable, an array or the contents of a pointer to a variable.   The simplest type of assignment expression is as follows:


a=1


where the variable on the left hand side is set to be equal to the value of the expression on the right hand side (in this case 1).      This expression can be made into a statement by adding a semi-colon to the end (see rules of syntax above):


a=1;


or it can by part of a larger expression


if(b eq (a=1)) ...


Assignment expressions are examples of expressions that have side effects since they alter the value of a variable.    Some assignment operators cause the side effect to happen before the expression is evaluated and some cause the side effect to happen after the expression has been evaluated.


Below is the set of assignment expressions supported by the TE language:


Expression

Effect

Side Effect

A = B

Put the value of B into A.

After

A += B

A = A + B

After

A -= B

A = A B

After

A /= B

A = A / B

After

A *= B       

A = A * B

After

A++

A = A + 1

Before

++A

A = A + 1

After

A--

A = A 1

        Before

--A

A = A 1

        After



Heres some examples to show the difference between those side effects that happen before and after the expression has been evaluated.


a=1;

b=++a;  // b will be set to 2


In the above example b will be set to 2 since the ++a happens before the expression is evaluated (so a is incremented then assigned to b)



a=1;

b=a++;    // b will be set to 1


Here b will be set to 1 since the a++ happens after the assignment expression is completed.