![]() |
![]() |
![]() |
![]() |
(..Expressions) | Contents | Index | (..Assignments (Var.)) |
|
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
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.)
... 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; |
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.
Like C, SCARLET offers abbreviations instead of these long forms. Generally they are given by
Note that there must not be a blank between
IntOp
(thus *,
/,
%,
+,
-,
<<,
>>,
&,
|) and
=.
This class of compounded assign operators (*=, /=, %=, ...)
we call IntAsgOp.
all.amount -= (others.amount / 23) * 23; others.amount %= 23;
Besides that, SCARLET provides own operators for increment (++) and decrement (-) of integer variables:
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!
![]() |
![]() |
![]() |
![]() |
(..Expressions) | Contents | Index | (..Assignments (Var.)) |