previous contents index next
(..Expressions) Contents Index (..Assignments (Var.))

2.11 Assignments to Variables (VarAssignment;)

VarAssignment - Syntax:

Syntax: VarAssignment

 

2.11.1 General Form

Talking of an assignment to a variable we mean the storage of a value in that part of the memory which is allocated to this variable. The value being there before is overwritten by this action.
The most general syntax of an assignment is given by

Var = Expression ;

It means that Expression is assigned to a variable with name Var. Both the variable and the value may be of type INT, STRING, BOOL, or CELL. The only thing that is important is that their types must be identical. (Remember that the variable type is already fixed at the beginning of a program. Because of that fact Var is replaced by IntVar, StringVar, ... in the syntax diagram above.)

(The semicolon is not part of VarAssignment, but belongs to superordinated parts of the grammar, which are not mentioned explicitely.)


Example 30:

The following example, which is - admittedly - not very closely connected with CA, should illustrate assignments to the different variable types. The lines shown here could (at least theoretically) be part of a RDL as well as of a SDL program:

  ...

VAR INT sum;
    CELL fishes, others, all;

BOOL ok;

REGISTER STRING species;     /* string register species */

INT amount;      /* num. register amount */
  ...

   fishes = {"carps", 23};
   others = {"water-fleas", 1842103};
   sum = fishes.amount;
   sum = sum + others.amount;
   all = {"inhabitants", sum};
   ok = all.amount == 1842126;

 

2.11.2 Short Forms

Observe that the syntax introduced above and especially the structure of Expression allows the variable name to appear on both sides of the equal sign; that means that the new value of Var is just a conversion of the old one.
 

Especially in connection with integer variables, assignments of this kind are often used. In the syntax below IntVar denotes twice the same variable.

IntVar = IntVar IntOp IntExpression ;

Like C, SCARLET offers abbreviations instead of these long forms. Generally they are given by

IntVar IntOp= IntExpression ;

Note that there must not be a blank between IntOp (thus *, /, %, +, -, <<, >>, &, |) and =. This class of compounded assign operators (*=, /=, %=, ...) we call IntAsgOp.


Example 30 (Resumption):

We could have replaced sum = sum + others.amount; with

sum += others.amount;

The situation after a fairly shared meal (for the carps, of course...):

all.amount -= (others.amount / 23) * 23;
others.amount %= 23;  

 

Besides that, SCARLET provides own operators for increment (++) and decrement (-) of integer variables:

IntVar ++ ;
IntVar -- ;

In the first case, 1 is added to the value of IntVar; in the second 1 is subtracted from it.

Remark:
Concerning increment and decrement SCARLET is stricter than C: although IntVar++ is a short form, it stays an assignment and can never become part of an IntExpression!


Example 30 (Resumption):

It seems that one of the carps has eaten too much...:

fishes.amount-;

previous contents index next
(..Expressions) Contents Index (..Assignments (Var.))